databound problems with c# datagrid .NET framework v. 1.1

O

Omatase

I have spent hours on the following two problems:


1 - Resizing a column to a new width
2 - disabling the addnew feature of the datagrid, without making cells
uneditable (i.e. by locking the control)


Here is the data I have on my progress so far:


I think the problems both stem from the fact that although I am using
the datagrid in databound mode, I am not databinding to a datatable. My

datasource is a class that just implements the ibindinglist interface.


Although I got off to a rocky start with the databinding with the
class, that part is working fine now.


Current Problem 1:
I cannot change the widths of the datagrid columns. I have read
numerous posts on this topic and am convinced that since I am not
binding to a datatable I must need to modify the class to which the
grid is bound. Here is the code I have that I am trying to resize the
column widths with.


<code>
dgAppCommands.TableStyles.Clea­r();


DataGridTableStyle dgStyle = new DataGridTableStyle();


dgStyle.MappingName = "ButtonCommandClass";


DataGridTextBoxColumn dgcStyle1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn dgcStyle2 = new DataGridTextBoxColumn();


dgcStyle1.MappingName = "Button";
dgcStyle2.MappingName = "Command";


dgcStyle2.Width = 25;


dgStyle.GridColumnStyles.Add ((DataGridColumnStyle)dgcStyle­1);
dgStyle.GridColumnStyles.Add ((DataGridColumnStyle)dgcStyle­2);


dgAppCommands.TableStyles.Add(­dgStyle);
</code>


Microsoft says the following in Visual Studio's help file "Set the grid

table object's MappingName to a DataTable object's TableName."


Since I don't have a "DataTable" I don't know what to set the
MappingName to. I have tried putting a property in the class that is
bound to the grid named "TableName" but that didn't work.


Current Problem 2:


I think the reason this doesn't work is also because I am binding to a
class, not a DataTable. Here is the code I have used:


<code>
//no adding of new rows thru dataview...
CurrencyManager cm =
(CurrencyManager)BindingContex­t[dgAppCommands.DataSource,
dgAppCommands.DataMember];


((DataView)cm.List).AllowNew = false;
</code>


I get a "Specified cast not valid" error on the last line of the above
snippet.


Any help with the above two problems would be greatly appreciated


-Dave
 
D

Dmytro Lapshyn [MVP]

Hi,

Regarding problem #2:
((DataView)cm.List).AllowNew = false;
</code>
I get a "Specified cast not valid" error on the last line of the above
snippet.

Change it to:

((IBindingList)cm.List).AllowNew = false;

given that you does not always return true from the AllowNew getter in your
IBindingList implementation.


Regarding problem #1:

Try to set the MappingName to the short name of the type which implements
the IBindingList interface.
I am saying this because MSDN docs suggest that one sets this property to
'ArrayList' when binding to an ArrayList instance.
Also, they suggest that one uses the 'classname[]' value, where classname is
the name of the class implementing the list.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


I have spent hours on the following two problems:


1 - Resizing a column to a new width
2 - disabling the addnew feature of the datagrid, without making cells
uneditable (i.e. by locking the control)


Here is the data I have on my progress so far:


I think the problems both stem from the fact that although I am using
the datagrid in databound mode, I am not databinding to a datatable. My

datasource is a class that just implements the ibindinglist interface.


Although I got off to a rocky start with the databinding with the
class, that part is working fine now.


Current Problem 1:
I cannot change the widths of the datagrid columns. I have read
numerous posts on this topic and am convinced that since I am not
binding to a datatable I must need to modify the class to which the
grid is bound. Here is the code I have that I am trying to resize the
column widths with.


<code>
dgAppCommands.TableStyles.Clea­r();


DataGridTableStyle dgStyle = new DataGridTableStyle();


dgStyle.MappingName = "ButtonCommandClass";


DataGridTextBoxColumn dgcStyle1 = new DataGridTextBoxColumn();
DataGridTextBoxColumn dgcStyle2 = new DataGridTextBoxColumn();


dgcStyle1.MappingName = "Button";
dgcStyle2.MappingName = "Command";


dgcStyle2.Width = 25;


dgStyle.GridColumnStyles.Add ((DataGridColumnStyle)dgcStyle­1);
dgStyle.GridColumnStyles.Add ((DataGridColumnStyle)dgcStyle­2);


dgAppCommands.TableStyles.Add(­dgStyle);
</code>


Microsoft says the following in Visual Studio's help file "Set the grid

table object's MappingName to a DataTable object's TableName."


Since I don't have a "DataTable" I don't know what to set the
MappingName to. I have tried putting a property in the class that is
bound to the grid named "TableName" but that didn't work.


Current Problem 2:


I think the reason this doesn't work is also because I am binding to a
class, not a DataTable. Here is the code I have used:


<code>
//no adding of new rows thru dataview...
CurrencyManager cm =
(CurrencyManager)BindingContex­t[dgAppCommands.DataSource,
dgAppCommands.DataMember];


((DataView)cm.List).AllowNew = false;
</code>


I get a "Specified cast not valid" error on the last line of the above
snippet.


Any help with the above two problems would be greatly appreciated


-Dave
 
O

Omatase

Dmytro,

You're awesome! your proposed solution to #1 worked great!

I have a question about what you suggested for solution #2.

It appears that the IBindingList type, although it *does* expose an
'AllowNew' property, only exposes that property as readonly. I have
also tried adding an 'AllowNew' get/set to my class and changing that
line of code to:

<code>
((ApplicationCommands)cm.List).AllowNew = false;
</code>

When I do I get no error, but the final '*' row remains. And when I
select that row it attempts to pull data from it, which of course it
doesn't yet exist so an error is thrown. What do you suggest I do at
this point? I am leaning towards somehow overriding the AllowNew in my
class for the IBindingList interface so I can change the AllowNew to
false there. But I don't even know if that's possible or how to do it.

Any help is much appreciated.
 
D

Dmytro Lapshyn [MVP]

I'm really glad to hear my suggestion worked!

Regarding IBindingList, does your implementation of the data source at all
need to add new rows?
If so, you might provide an explicit implementation of IBindingList *and*
expose a public AllowNew property which would really govern what
IBindingList.AllowNew returns:

public class MyDataSource: IBindingList
{
private bool _allowNew = true;

// Other IBindingList members ...
bool IBindingList.AllowNew
{
// This one is read-only and we cannot change it as we are
implementing an interface defined elsewhere.
get { return _allowNew; }
}

// Now go public properties of the class itself.

// We make this property read-write so we can change the AllowNew
setting returned by the interface implementation.
public bool AllowNew
{
get {return _allowNew; }
set {_allowNew = value; }
}
}

Now, change the previously suggested line:
((IBindingList)cm.List).AllowNew = false;

to

((MyDataSource)cm.List).AllowNew = false;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top