Binding Dataset to DataGridView
-
I am attemping to bind the data from an xml file into a datagridview. I have achieved this via the following code:
quotesDataSet.ReadXml(Properties.Settings.Default.XmlQuoteFile); quoteGrid.DataSource = quotesDataSet; quoteGrid.DataMember = "quote";
This automatically binds the four elements of the quote parent into four rows. However, from that point on, I cannot rename the header rows or control them in any way. I've tried everything from attempting to assign a datagridtablestyle, to simply using quoteGrid.Columns[1].HeaderText = "Text"; Can anyone provide me with a working example of how to achieve this? -
I am attemping to bind the data from an xml file into a datagridview. I have achieved this via the following code:
quotesDataSet.ReadXml(Properties.Settings.Default.XmlQuoteFile); quoteGrid.DataSource = quotesDataSet; quoteGrid.DataMember = "quote";
This automatically binds the four elements of the quote parent into four rows. However, from that point on, I cannot rename the header rows or control them in any way. I've tried everything from attempting to assign a datagridtablestyle, to simply using quoteGrid.Columns[1].HeaderText = "Text"; Can anyone provide me with a working example of how to achieve this?I think it makes sense that you can not change the header because you are directly linking the xml file to the datagrid. To change the header you need to create custom columns using DataGridTextBoxColumn classes (example follows). Another option you might have is to read the xml into a dataset and modify the table with your data so it has a different column name. Example for custom table: Instead of setting Datasource, use the following:
this.dataGrid1.SetDataBinding(myDataSet, "MyTable"); CreateCustomTable();
private void CreateCustomTable() { DataGridTableStyle ts1 = new DataGridTableStyle(); ts1.MappingName = "Department"; // Set other properties. ts1.AlternatingBackColor = Color.LightGray; // Add column DataGridColumnStyle TextCol = new DataGridTextBoxColumn(); TextCol.MappingName = "Name"; TextCol.HeaderText = "Department Name"; TextCol.Width = this.dataGrid1.Size.Width-60; ts1.GridColumnStyles.Add(TextCol); //add more columns as needed and map directly to any column of your table. //lastly, add the tableStyle this.dataGrid1.TableStyles.Add(ts1); } -
I think it makes sense that you can not change the header because you are directly linking the xml file to the datagrid. To change the header you need to create custom columns using DataGridTextBoxColumn classes (example follows). Another option you might have is to read the xml into a dataset and modify the table with your data so it has a different column name. Example for custom table: Instead of setting Datasource, use the following:
this.dataGrid1.SetDataBinding(myDataSet, "MyTable"); CreateCustomTable();
private void CreateCustomTable() { DataGridTableStyle ts1 = new DataGridTableStyle(); ts1.MappingName = "Department"; // Set other properties. ts1.AlternatingBackColor = Color.LightGray; // Add column DataGridColumnStyle TextCol = new DataGridTextBoxColumn(); TextCol.MappingName = "Name"; TextCol.HeaderText = "Department Name"; TextCol.Width = this.dataGrid1.Size.Width-60; ts1.GridColumnStyles.Add(TextCol); //add more columns as needed and map directly to any column of your table. //lastly, add the tableStyle this.dataGrid1.TableStyles.Add(ts1); }I'm working with the new Visual C# Express beta and the latest of the .Net 2 version. Unfortunately, they have replaced datagrid with datagridview, and datagridtablestyle does not seem to have a new version. Unfortunately, they also have not provided any documentation that I can understand for achieving the same effects. Any other thoughts? Thanks for the help either way.
-
I'm working with the new Visual C# Express beta and the latest of the .Net 2 version. Unfortunately, they have replaced datagrid with datagridview, and datagridtablestyle does not seem to have a new version. Unfortunately, they also have not provided any documentation that I can understand for achieving the same effects. Any other thoughts? Thanks for the help either way.
-
In that case instead of
quoteGrid.Columns[1].HeaderText
tryyourDataGridView.Columns[1].Name="your column name"
. I hope that helps, I really haven't tried the new version yet.