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. DataGrid Set Column Width

DataGrid Set Column Width

Scheduled Pinned Locked Moved C#
csharpdatabasetutoriallearning
4 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
    mfcuser
    wrote on last edited by
    #1

    I want to know how to use DataGridColumnStyle to set the width of my datagrid. I know how to use PreferredColumnWidth to set the width of the oveall colunm, but it is not what I want. I want to set individual column. Here is my program I also want to know if there is a good documentation for the .net classes. While I was working in the DataGridClass. My class is defined as dataGrid1, from dataGrid1, there is not way to know that DataGridColumnStyle exists. I want to know is there is a good book or documentation tha point out intaction between associated classes like for instance DataGrid and DataGridColumnStyle. private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); dataGrid1.PreferredColumnWidth = 100;//set the overall column width for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }

    H 1 Reply Last reply
    0
    • M mfcuser

      I want to know how to use DataGridColumnStyle to set the width of my datagrid. I know how to use PreferredColumnWidth to set the width of the oveall colunm, but it is not what I want. I want to set individual column. Here is my program I also want to know if there is a good documentation for the .net classes. While I was working in the DataGridClass. My class is defined as dataGrid1, from dataGrid1, there is not way to know that DataGridColumnStyle exists. I want to know is there is a good book or documentation tha point out intaction between associated classes like for instance DataGrid and DataGridColumnStyle. private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); dataGrid1.PreferredColumnWidth = 100;//set the overall column width for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      If you want to know if there's good documentation, then read it and determine whether you think it's good. You can set the width of a specific column using a DataGridColumnStyle that you add to a DataGridTableStyle for a specific DataTable (or type, if reflecting against something other than a DataSet or DataTable), which you add to DataGrid.TableStyles. You should read the documentation, which also presents articles. The DataGridColumnStyle does exactly what you're asking and is specific to the column it represents in a DataGrid. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

      M 1 Reply Last reply
      0
      • H Heath Stewart

        If you want to know if there's good documentation, then read it and determine whether you think it's good. You can set the width of a specific column using a DataGridColumnStyle that you add to a DataGridTableStyle for a specific DataTable (or type, if reflecting against something other than a DataSet or DataTable), which you add to DataGrid.TableStyles. You should read the documentation, which also presents articles. The DataGridColumnStyle does exactly what you're asking and is specific to the column it represents in a DataGrid. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]

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

        After adding all these extra codes, it still doesn't work. It shouldn't take all too much effort jsut to increse or decrease the length of a column in a grid. In terms of the documentation, I think it would have been bette to show interaction from one class to another. private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; DataGridTableStyle ts1 = new DataGridTableStyle(); DataGridColumnStyle boolCol = new DataGridBoolColumn(); dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); ts1.MappingName = "Index"; boolCol.MappingName = "Index"; boolCol.Width = 20; ts1.GridColumnStyles.Add(boolCol); dataGrid1.TableStyles.Add(ts1); for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }

        H 1 Reply Last reply
        0
        • M mfcuser

          After adding all these extra codes, it still doesn't work. It shouldn't take all too much effort jsut to increse or decrease the length of a column in a grid. In terms of the documentation, I think it would have been bette to show interaction from one class to another. private void Form1_Load(object sender, System.EventArgs e) { double[] dData1 = new double[100]; double[] dData2 = new double[100]; DataTable dt = new DataTable(); DataRow dr; DataGridTableStyle ts1 = new DataGridTableStyle(); DataGridColumnStyle boolCol = new DataGridBoolColumn(); dt.Columns.Add(new DataColumn("Index", typeof(int))); dt.Columns.Add(new DataColumn("Sinve Values", typeof(float))); dt.Columns.Add(new DataColumn("Cosine Values", typeof(float))); ts1.MappingName = "Index"; boolCol.MappingName = "Index"; boolCol.Width = 20; ts1.GridColumnStyles.Add(boolCol); dataGrid1.TableStyles.Add(ts1); for(int i=0;i<100;i++) { dData1[i] = Math.Sin(0.1*i); dData2[i] = Math.Cos(0.2*i); dr = dt.NewRow(); dt.Rows.Add(dr); dr[0] = i;; dr[1] = dData1[i]; dr[2] = dData2[i]; } dataGrid1.DataSource = new DataView(dt); waveformPlot1.PlotY(dData1); waveformPlot2.PlotY(dData2); }

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          It's not difficult, but your problem is that you need to name your DataTable. The DataGrid associates a particular DataGridTableStyle with a DataGrid by matching the DataTable.TableName (you can pass this to the constructor) with one of the DataGridTableStyle's MappingName that was added to the DataGrid.TableStyles property. Your modified code below works fine:

          double\[\] dData1 = new double\[100\];
          double\[\] dData2 = new double\[100\];
          

          DataTable dt = new DataTable("Test");
          DataRow dr;
          DataGridTableStyle ts1 = new DataGridTableStyle();
          ts1.MappingName = dt.TableName;
           
          DataGridColumnStyle boolCol = new DataGridBoolColumn();
           
          dt.Columns.Add("Index", typeof(int));
          dt.Columns.Add("Sinve Values", typeof(float));
          dt.Columns.Add("Cosine Values", typeof(float));
           
          boolCol.MappingName = "Index";
          boolCol.Width = 20;
          ts1.GridColumnStyles.Add(boolCol);
          dataGrid1.TableStyles.Add(ts1);
           
          for (int i=0; i<100; i++)
          {
          dData1[i] = Math.Sin(0.1*i);
          dData2[i] = Math.Cos(0.2*i);
          dr = dt.NewRow();
          dt.Rows.Add(dr);
          dr[0] = i;
          dr[1] = dData1[i];
          dr[2] = dData2[i];
          }
           
          dataGrid1.DataSource = dt;

          You did, however, forget to add column styles for your other columns. When you use table styles with defined columns (even just 1), no columns are auto-generated. Besides, using a DataTable here is unnecessary. A simple array of some struct like so would work:

          public struct Data
          {
          public int Index;
          public double Sinve;
          public double Cosine;
          }

          In this case, you set DataGridTableStyle.MappingName to "Data". This is documented in the DataGridTableStyle.MappingName property documentation. Also, why are you binding a DataGridBooleanColumn to an int DataColumn? That's going to set it to either 0 or 1 and will start (for all but the first row) in an indeterminate state. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog

          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