Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Binding Dataset to DataGridView

Binding Dataset to DataGridView

Scheduled Pinned Locked Moved C#
tutorialwpfwcfxmlquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikan23
    wrote on last edited by
    #1

    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?

    R 1 Reply Last reply
    0
    • M Mikan23

      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?

      R Offline
      R Offline
      rudy net
      wrote on last edited by
      #2

      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); }

      M 1 Reply Last reply
      0
      • R rudy net

        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); }

        M Offline
        M Offline
        Mikan23
        wrote on last edited by
        #3

        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.

        R 1 Reply Last reply
        0
        • M Mikan23

          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.

          R Offline
          R Offline
          rudy net
          wrote on last edited by
          #4

          In that case instead of quoteGrid.Columns[1].HeaderText try yourDataGridView.Columns[1].Name="your column name". I hope that helps, I really haven't tried the new version yet.

          M 1 Reply Last reply
          0
          • R rudy net

            In that case instead of quoteGrid.Columns[1].HeaderText try yourDataGridView.Columns[1].Name="your column name". I hope that helps, I really haven't tried the new version yet.

            M Offline
            M Offline
            Mikan23
            wrote on last edited by
            #5

            Its determined to stay with the fields in the dataset, so I tried your second suggestion from the original and renamed the fields in the dataset temporarily. This worked on the datagridview just fine, many thanks!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups