473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Multiple problems with WinForms DataGrid

I have a Windows Forms datagrid with the datasource set to an ArrayList.

The ArrayList is initially empty.

I have DataGridColumn styles defined for 4 columns with widths, header
titles, and mapping names set.

The problems:

1: Now, I realize ArrayList doesn't implement IBindingList, so the
CurrencyManager won't get updated when I add items to the array.

I was under the impression I could simply do:

CurrencyManager cm = BindingContext[myDataGrid.DataSource] as
CurrencyManager;
cm.Refresh()

and that would fix it. Even though according to the watch window, I have 4
visible columns and 1 visible row after adding the row and calling
CurrencyManager.Refresh. Calling DataGrid.Refresh() does nothing for this
either.

The only solution I've found is to do something like:

grid.DataSource = null;
grid.DataSource = myArray;

But that seems stupid.

2: Column headers don't display unless there's data in the array. Is there
any way around this? I'd like them to display all the time because when
there's no data, it's just a big square empty control.

3: Using the code from George Shepard's Windows Forms FAQ, #5.11, I have the
grid set up so that clicking on a row selects the entire row on mouse up.

The problem is, when you press the mouse on a cell and don't let up, the
cell is selected. I tried duplicating the code in the MouseDown event, but
that doesn't work. Any ideas on how to fix that?

Thanks.

Pete
Nov 17 '05 #1
2 2822
Pete,

See inline.
1: Now, I realize ArrayList doesn't implement IBindingList, so the
CurrencyManager won't get updated when I add items to the array.

I was under the impression I could simply do:

CurrencyManager cm = BindingContext[myDataGrid.DataSource] as
CurrencyManager;
cm.Refresh()

and that would fix it. Even though according to the watch window, I have 4
visible columns and 1 visible row after adding the row and calling
CurrencyManager.Refresh. Calling DataGrid.Refresh() does nothing for this
either.

The only solution I've found is to do something like:

grid.DataSource = null;
grid.DataSource = myArray;

But that seems stupid.
You can create a wrapper around the array list which implements
IBindingList. You would have to expose the members of array list that are
relevant (like Add, Insert, Remove, and access to the indexer), but all in
all, it's not too hard.

I'm assuming you are not using .NET 2.0. If you were, you could use the
DataGridView, and the BindingList<T> class, which implements the
IBindingList interface.
2: Column headers don't display unless there's data in the array. Is there
any way around this? I'd like them to display all the time because when
there's no data, it's just a big square empty control.
Yeah, that's a big gripe. Unfortunately, short of custom painting or a
third-party control, I don't know if there is anything you can do about it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

3: Using the code from George Shepard's Windows Forms FAQ, #5.11, I have
the grid set up so that clicking on a row selects the entire row on mouse
up.

The problem is, when you press the mouse on a cell and don't let up, the
cell is selected. I tried duplicating the code in the MouseDown event, but
that doesn't work. Any ideas on how to fix that?

Thanks.

Pete

Nov 17 '05 #2
> Pete,

See inline.
1: Now, I realize ArrayList doesn't implement IBindingList, so the
CurrencyManager won't get updated when I add items to the array.

I was under the impression I could simply do:

CurrencyManager cm = BindingContext[myDataGrid.DataSource] as
CurrencyManager;
cm.Refresh()
[snip]
You can create a wrapper around the array list which implements
IBindingList. You would have to expose the members of array list that are
relevant (like Add, Insert, Remove, and access to the indexer), but all in
all, it's not too hard.

I'm assuming you are not using .NET 2.0. If you were, you could use
the DataGridView, and the BindingList<T> class, which implements the
IBindingList interface.


No, .NET 1.1. Actually, in fixing #2, I fixed this one as well, kind of by
accident. See below how...

2: Column headers don't display unless there's data in the array. Is
there any way around this? I'd like them to display all the time because
when there's no data, it's just a big square empty control.


Yeah, that's a big gripe. Unfortunately, short of custom painting or a
third-party control, I don't know if there is anything you can do about
it.

[snip]

In digging through the DataGrid code with Reflector, I determined that the
column headers weren't painting because the CurrencyManager can't get
PropertyDescriptors from the empty array. Why it needs them to draw the
information I've already provided in the ColumnHeaderStyles is beyond me,
but nevertheless, it apparently needs the property descriptors.

Attaching property descriptors to the ColumnHeaderStyles didn't work because
the grid just cleared them out.

What did work was to add a single dummy item to my array, bind it to the
grid, then remove the item from the array and call
CurrencyManager.Refresh().

By doing this, the grid was able to find PropertyDescriptors for the
columns, but deleting the row didn't delete the property descriptors, so the
column headers painted properly.

The lucky side-effect of this was that adding rows later and calling
CurrencyManager.Refresh() also worked, so #1 was fixed as well..

3: Using the code from George Shepard's Windows Forms FAQ, #5.11, I have
the grid set up so that clicking on a row selects the entire row on mouse
up.

The problem is, when you press the mouse on a cell and don't let up, the
cell is selected. I tried duplicating the code in the MouseDown event,
but that doesn't work. Any ideas on how to fix that?


Any ideas on this? I'm thinking I'm going to have to derive a class from
DataGrid and handle the MouseDown event. I see more digging in Reflector in
my future.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Richard Payne | last post by:
I am developing a web application that utilises multiple grids, panels, validation controls etc on the same page. I have added the appropriate code to pop up dialogs to confirm delete in...
5
by: John Spiegel | last post by:
Hey all, I have contusions and I think a minor concussion after trying to find information on advanced WinForms DataGrid use. Does anyone know of a site / book that gets really in-depth on the...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
5
by: BBFrost | last post by:
Win2000 ..Net 1.1 SP1 c# using Visual Studio Ok, I'm currently in a "knock down - drag out" tussle with the .Net 1.1 datagrid. I've come to realize that a 'block' of rows highlighted within...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: Eddy Balan | last post by:
Hi. Please help me....... I would like to copy the datagrid contents to clipboard and then to paste it into Excel but I can't make a multiple selection. Eddy
2
by: Bill nguyen | last post by:
I've been using Datagrid for most of my app's data entry screens. Now I have the need for users to select multiple rows for printing. Is it possible with Datagrid items? Thanks Bill
4
by: Floppy Jellopy | last post by:
Hello, I was recently asked to look at someones code to see if i could determine why a particular form was failing to load and instead generating an error about a lack of available resources for...
1
by: TonyJ | last post by:
Hello! If there is a datagrid in VS2005 for winforms what kind of datagrid is it then. 1.Is it a bound datagrid that has a direct connection to columns for tables in the database. 2. Is it an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.