Share via


Working with the Datagrid Control in Visual Basic .NET

Posted August 8, 2003

Chat Date: August 5, 2003

Please note: Portions of this transcript have been edited for clarity

Chat Participants:

  • Jayaprakasam, Support Engineer in Webdata.NET team
  • Scott, tester with the .Net client team
  • Steve Stein, Programmer/Writer on the VB Team
  • Harish Kantameri, VB compiler team
  • Bina Shukla, developer support engineer for winform

Moderator: Jason (Microsoft)
Welcome to today’s chat on working with the Datagrid Control in Visual Basic .NET. We have members of the Visual Basic and Windows Forms teams here to answer your questions today. I will ask the hosts to introduce themselves.

Host: JP (Microsoft)
I am Jayaprakasam working as a Support Engineer in Webdata.NET team

Host: Scott (Microsoft)
Hey. I'm Scott, a tester with the .Net client team.

Host: Steve (Microsoft)
Hi all - I'm Steve Stein, a Programmer/Writer on the VB Team.

Host: Bina (Microsoft)
Hi I'm Bina Shukla. I'm developer support engineer for winforms.

Moderator: Jason (Microsoft)
And I am Jason Cooke, the VB.NET chat coordinator. Glad you all could make it today!

Let's get started! Fire away with your questions for our hosts.

Host: Scott (Microsoft)
Q: How to prevent DataGrid jump to front when updating data?

A: Sorry, I don't believe there's a way to do this. It's a known bug with no current solution.

Host: Scott (Microsoft)
Q: What are the major differences between the grid used in ASP.Net and the one used in Windows apps?

A: The Windows Forms datagrid works with an IList and is normally read/write. The ASP.Net datagrid works with IEnumerable (which is a superset of IList), but it doesn't handle the writes for you (if you want read/write, you have to do the updating logic yourself). And there's a link for the differences at https://msdn.microsoft.com/en-us/library/aa289519(v=VS.71).aspx

Host: Steve (Microsoft)
Q: On a single form why can’t we bind two datagrid from same datasource?

A: You can, just set the Datasource and Datamember properties to the same datasource.

Host: Scott (Microsoft)
Q: How to implement frozen Row or Column?

A: Sorry, there's no nice answer today. We're looking to address it in a future version. (You could probably hack around the frozen rows/columns by using your own DataGridColumnStyles, but it would be neither pretty nor easy.)

Host: Bina (Microsoft)
Q: How to align text in Column header?

A: You would have to use ColumnStyles and the DataGridColumnStyle.Alignment Property.

Host: Steve (Microsoft)
Q: Can group function like Summation,count, average, min, max of column data be displayed in the last row in datagrid?

A: You can add an Expression column to the datasource to display aggregate information like Sum, Count, etc.

Host: Steve (Microsoft)
Q: I think Expression column is for column , not for row.

A: When you add a new Column to a DataTable it appears in every row. Just set the Expression property (DataColumn.Expression) to perform the desired calculation. Check out the DataColumn.Expression topic: https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=VS.71).aspx

Host: Scott (Microsoft)
Q: How to set datacells in different style?

A: You'd have to define a DataGridColumnStyle that allows you to set different backcolor, forecolor, etc. If you want to whisper me, I can email you an implementation that uses event andlers to do it.

Host: JP (Microsoft)
Q: Do you know of any way to keep my data grid from filling the data while I work on other parts of my form?

A: You can use a background thread. 318604 HOW TO: Populate Datagrid on Background Thread with Data Binding by https://support.microsoft.com/default.aspx?scid=kb;en-us;318604

Host: Scott (Microsoft)
Q: how to insert Row correctly? the function call Insert Row seems appending the row.

A: If you mean to have a particular order, you should bind to a DataView and set the Sort on it.

Host: Steve (Microsoft)
Q: When I want a total of rows, it seems can't be implemented by Expression Column.

A: The Expression column can aggregate information from related tables. For example, if you have a row that is a customer record, you can add an expression column that hsows the total of all orders in a related table. The link I gave above gives an example of how to do this.

Host: Steve (Microsoft)
You can also check out this paper that shows how to create expression columns that do just this - https://msdn.microsoft.com/en-us/library/aa289510(v=VS.71).aspx

Host: JP (Microsoft)
For Article Q318604, use https://support.microsoft.com/?id=318604

Host: Steve (Microsoft)
Q: Can we display hierarchical data representation in datagrid?

A: Parent/Child relations automatically offer an 'expando' icon which allows you to drill into the related hierarchical records.

Host: Scott (Microsoft)
Q: How to paint Column header?

A: You could fill a rectangle with HeaderBackColor and draw in the string. I don't believe there's an easier way.

Host: Scott (Microsoft)
Q: So which method I can override to paint column header?

A: You'd add a handler for the Paint event (that'll happen after the grid's done with it's painting, so you'll be painting over the top of what's there).

Moderator: Jason (Microsoft)
Q: Is merging of cells is possible?

A: No, that cannot be done with the DataGrid. Merging is a very dificult problem.

Host: Scott (Microsoft)
Q: Can data from datagrid be bound to graphical control if any? i mean can datagrid's data be represented in graphical form or in the form of charts?

A: I'd suspect that most graphical controls would take a DataTable as a possible input, so you'd get it if you set thew DataGrid's DataSource to the same table.

Host: Scott (Microsoft)
(Side note: the whole faq's worth looking over: it's at https://www.syncfusion.com/FAQ/WinForms.

Moderator: Jason (Microsoft)
This has been a GREAT chat. Thank you to everyone. Unfortunately, it is time to go. Thanks for participating, and we'll see you next time!

For further information on this topic please visit the following:

Newsgroups: microsoft.public.dotnet.languages.vb

VB .NET Transcripts: Read the archive of past VB .NET chats.

Website: Visit the Microsoft Visual Basic .NET site.

Top of pageTop of page