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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Changing Datagrid elements programmically in C#

Changing Datagrid elements programmically in C#

Scheduled Pinned Locked Moved C#
csharptutorialcsswpfwcf
3 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.
  • V Offline
    V Offline
    Vernware
    wrote on last edited by
    #1

    I am using the Datagrid form control in C#.net environment. I have no problem binding the datagrid to the datasource and displaying the table I want to display. But I am having a problem changing some of the grid elements using code after I have displayed the control. For example, there are some columns that need the width changed. I have not been able to change any of the grid elements/properties at all. Here is the code that displays the grid and its data: ------------------------------------------------------------------------------ DataSet aDataSet = new DataSet(); OleDbDataAdapter aAdaptor; aSqlQuery = "SELECT * From Employee"; aConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID='Admin';Data Source=CSharpDataGrid.mdb;"; aConnection = new OleDbConnection(aConnectionString); aAdaptor = new OleDbDataAdapter(aSqlQuery, aConnection); aAdaptor.Fill(aDataSet); this.dgEmployee.DataSource = aDataSet.Tables[0]; -------------------------------------------------------------------------- As I said, this code brings up the control with the data loaded. Now I am trying to change the width of one column: -------------------------------------------------------------------------- DataGridTableStyle EmployeeTableStyle = new DataGridTableStyle(); EmployeeTableStyle.MappingName = "Employee"; DataGridColumnStyle ColumnFullName = new DataGridTextBoxColumn(); ColumnFullName.MappingName = "Name"; ColumnFullName.HeaderText = "Full Name"; ColumnFullName.Width = 5; EmployeeTableStyle.GridColumnStyles.Add(ColumnFullName); ----------------------------------------------------------------------- This code has no effect on the displayed grid. I would expect the DataGridTableStyle to be associated with the Employee table in the grid dataset. And then I would expect the column “Name” would change as per the above code. But nothing happens. Would appreciate any tips on how to change row and column properties (width, color, selection status, etc) using C# code. Thanks, Vern Vern

    G 1 Reply Last reply
    0
    • V Vernware

      I am using the Datagrid form control in C#.net environment. I have no problem binding the datagrid to the datasource and displaying the table I want to display. But I am having a problem changing some of the grid elements using code after I have displayed the control. For example, there are some columns that need the width changed. I have not been able to change any of the grid elements/properties at all. Here is the code that displays the grid and its data: ------------------------------------------------------------------------------ DataSet aDataSet = new DataSet(); OleDbDataAdapter aAdaptor; aSqlQuery = "SELECT * From Employee"; aConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;User ID='Admin';Data Source=CSharpDataGrid.mdb;"; aConnection = new OleDbConnection(aConnectionString); aAdaptor = new OleDbDataAdapter(aSqlQuery, aConnection); aAdaptor.Fill(aDataSet); this.dgEmployee.DataSource = aDataSet.Tables[0]; -------------------------------------------------------------------------- As I said, this code brings up the control with the data loaded. Now I am trying to change the width of one column: -------------------------------------------------------------------------- DataGridTableStyle EmployeeTableStyle = new DataGridTableStyle(); EmployeeTableStyle.MappingName = "Employee"; DataGridColumnStyle ColumnFullName = new DataGridTextBoxColumn(); ColumnFullName.MappingName = "Name"; ColumnFullName.HeaderText = "Full Name"; ColumnFullName.Width = 5; EmployeeTableStyle.GridColumnStyles.Add(ColumnFullName); ----------------------------------------------------------------------- This code has no effect on the displayed grid. I would expect the DataGridTableStyle to be associated with the Employee table in the grid dataset. And then I would expect the column “Name” would change as per the above code. But nothing happens. Would appreciate any tips on how to change row and column properties (width, color, selection status, etc) using C# code. Thanks, Vern Vern

      G Offline
      G Offline
      gnadeem
      wrote on last edited by
      #2

      hi, the default name of your table in aDataSet is 'Table'. this should match with your EmployeeTableStyle.MappingName which is 'Employee'. set the aDataSet.Tables[0].TableName='Employee'. if you are not skipping any code, then add styles in following order and THEN BIND. DataGridColumnsStyle(s) --> DataGridTableStyle --> TableStyles regards

      V 1 Reply Last reply
      0
      • G gnadeem

        hi, the default name of your table in aDataSet is 'Table'. this should match with your EmployeeTableStyle.MappingName which is 'Employee'. set the aDataSet.Tables[0].TableName='Employee'. if you are not skipping any code, then add styles in following order and THEN BIND. DataGridColumnsStyle(s) --> DataGridTableStyle --> TableStyles regards

        V Offline
        V Offline
        Vernware
        wrote on last edited by
        #3

        You really identified the problem. Once I set the aDataSet table name to "Employee", everything suddenly works. I spent days working on this problem and got so frustrated. I am so thankful for your tip. Such a little detail can make such a difference. Thanks again for your help. Vern

        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