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. Setting Colum widths in a datagrid

Setting Colum widths in a datagrid

Scheduled Pinned Locked Moved C#
css
7 Posts 3 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.
  • P Offline
    P Offline
    Peter8675309
    wrote on last edited by
    #1

    I am programatically building a dataset that populates a datagrid. I need to then specfify the column widths for each column. However, the grid control ignores my input. I first tried setting the widths under TableStyles/GridColumnStyles. That was ignored so I figured it was because I wasn't populating the grid from a predefined datasource. So then I tried progammatically. Still nothing. Below is my code. The grid should only have one table in it. The grid populates correctly, it just doesn't format the widths of the columns. The dataset only returns 3 columns.:confused: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true;

    L 1 Reply Last reply
    0
    • P Peter8675309

      I am programatically building a dataset that populates a datagrid. I need to then specfify the column widths for each column. However, the grid control ignores my input. I first tried setting the widths under TableStyles/GridColumnStyles. That was ignored so I figured it was because I wasn't populating the grid from a predefined datasource. So then I tried progammatically. Still nothing. Below is my code. The grid should only have one table in it. The grid populates correctly, it just doesn't format the widths of the columns. The dataset only returns 3 columns.:confused: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true;

      L Offline
      L Offline
      Looney Tunezez
      wrote on last edited by
      #2

      I am assuming that its a winform application: Peter8675309 wrote: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); dgResults.TableStyles.Clear(); DataTableStyle tabStyle1=new DataTableStyle(); tabStyle1.MappingName = //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; dgResults.TableStyles.Add(tabStyle1); //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true; try this :

      //get the data
      dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText));
      
      //populate the grid
      dgResults.DataSource = dsResults.Tables[0]; <<-- maybe!!!
      
      //settings for grid
      dgResults.TableStyles[0].GridColumnStyles[0].Width=200;
      dgResults.TableStyles[0].GridColumnStyles[1].Width=50;
      dgResults.TableStyles[0].GridColumnStyles[2].Width=100;
      dgResults.ReadOnly=true;
      

      "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
      this.Dispose();
      "A Bug is a piece of code that knows whatz its purpose"

      P 1 Reply Last reply
      0
      • L Looney Tunezez

        I am assuming that its a winform application: Peter8675309 wrote: //get the data dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText)); dgResults.TableStyles.Clear(); DataTableStyle tabStyle1=new DataTableStyle(); tabStyle1.MappingName = //populate the grid dgResults.DataSource = dsResults.Tables[0].DefaultView; dgResults.TableStyles.Add(tabStyle1); //settings for grid dgResults.TableStyles[0].GridColumnStyles[0].Width=200; dgResults.TableStyles[0].GridColumnStyles[1].Width=50; dgResults.TableStyles[0].GridColumnStyles[2].Width=100; dgResults.ReadOnly=true; try this :

        //get the data
        dsResults = SqlHelper.ExecuteDataset(Conn,CommandType.StoredProcedure,"usp_GetSearchResultsSel",new SqlParameter("@Description", txtDescription.Text),new SqlParameter("@SearchParameters", cboSearchParam.SelectedText));
        
        //populate the grid
        dgResults.DataSource = dsResults.Tables[0]; <<-- maybe!!!
        
        //settings for grid
        dgResults.TableStyles[0].GridColumnStyles[0].Width=200;
        dgResults.TableStyles[0].GridColumnStyles[1].Width=50;
        dgResults.TableStyles[0].GridColumnStyles[2].Width=100;
        dgResults.ReadOnly=true;
        

        "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
        this.Dispose();
        "A Bug is a piece of code that knows whatz its purpose"

        P Offline
        P Offline
        Peter8675309
        wrote on last edited by
        #3

        Yes this is a windows form. If I add in dgResults.TableStyles[0].GridColumnStyles.Clear(); I get an 'index out of range' error unless I prepopulate the TableStyles of the grid prior. I took a look at the code at section 5.3.2 from http://www.codeproject.com/csharp/PracticalGuideDataGrids4.asp#\_Toc56951053. This functions (if I set the dt equal to me dataset.Column), but it still doesn't modify the column widths. X| Thanks for your quick reply.

        L 1 Reply Last reply
        0
        • P Peter8675309

          Yes this is a windows form. If I add in dgResults.TableStyles[0].GridColumnStyles.Clear(); I get an 'index out of range' error unless I prepopulate the TableStyles of the grid prior. I took a look at the code at section 5.3.2 from http://www.codeproject.com/csharp/PracticalGuideDataGrids4.asp#\_Toc56951053. This functions (if I set the dt equal to me dataset.Column), but it still doesn't modify the column widths. X| Thanks for your quick reply.

          L Offline
          L Offline
          Looney Tunezez
          wrote on last edited by
          #4

          Peter8675309 wrote: dgResults.TableStyles[0].GridColumnStyles.Clear(); clear the table styles, not the gridcolumnstyles. Well it seems to work for me:

          this.dataGrid1.DataSource=null;
          this.dataGrid1.TableStyles.Clear();

          DataTableStyles tab1=new DataTableStyles();
          tab1.MappingName="ShoppingTables";

          this.dataGrid1.TableStyles.Add(tab1);

          //Sets the data source of the grid to the data table returned

          this.dataGrid1.DataSource= DbHelper.GetDataSet(blah...blah).Tables[0];

          this.dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 50;
          this.dataGrid1.TableStyles[0].GridColumnStyles[1].Width = 0; //hide column

          this.dataGrid1.AllowNavigation = false;
          this.dataGrid1.AllowSorting = false;

          if( this.dataGrid1.VisibleRowCount > 0)
          this.dataGrid1.Select(0);

          Let me know if this works for you, in my case i am making the width of of 1st column to 50 and 2nd column hidden. "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
          this.Dispose();
          "A Bug is a piece of code that knows whatz its purpose"

          P 1 Reply Last reply
          0
          • L Looney Tunezez

            Peter8675309 wrote: dgResults.TableStyles[0].GridColumnStyles.Clear(); clear the table styles, not the gridcolumnstyles. Well it seems to work for me:

            this.dataGrid1.DataSource=null;
            this.dataGrid1.TableStyles.Clear();

            DataTableStyles tab1=new DataTableStyles();
            tab1.MappingName="ShoppingTables";

            this.dataGrid1.TableStyles.Add(tab1);

            //Sets the data source of the grid to the data table returned

            this.dataGrid1.DataSource= DbHelper.GetDataSet(blah...blah).Tables[0];

            this.dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 50;
            this.dataGrid1.TableStyles[0].GridColumnStyles[1].Width = 0; //hide column

            this.dataGrid1.AllowNavigation = false;
            this.dataGrid1.AllowSorting = false;

            if( this.dataGrid1.VisibleRowCount > 0)
            this.dataGrid1.Select(0);

            Let me know if this works for you, in my case i am making the width of of 1st column to 50 and 2nd column hidden. "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
            this.Dispose();
            "A Bug is a piece of code that knows whatz its purpose"

            P Offline
            P Offline
            Peter8675309
            wrote on last edited by
            #5

            Where is datatablestyles defined? I don't see it in the help of on MS's site. How is this class defined? If it comes from WebControls, I am working on a windows form. Thanks for your help.

            L 1 Reply Last reply
            0
            • P Peter8675309

              Where is datatablestyles defined? I don't see it in the help of on MS's site. How is this class defined? If it comes from WebControls, I am working on a windows form. Thanks for your help.

              L Offline
              L Offline
              Looney Tunezez
              wrote on last edited by
              #6

              Peter8675309 wrote: datatablestyles I sincerely apologize for misleading you by datatablestyles. its actually DataGridTableStyles. Its part of the System.Windows.Forms namespace. Hope this helps, again i am sorry for the wrong class name fiasco!:((:-O "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
              this.Dispose();
              "A Bug is a piece of code that knows whatz its purpose"

              A 1 Reply Last reply
              0
              • L Looney Tunezez

                Peter8675309 wrote: datatablestyles I sincerely apologize for misleading you by datatablestyles. its actually DataGridTableStyles. Its part of the System.Windows.Forms namespace. Hope this helps, again i am sorry for the wrong class name fiasco!:((:-O "Creating tomorrow's legacy systems today..... .... One CRISIS at a time!" -- Unknown "If you build it.... .....BUGS will come!" -JB
                this.Dispose();
                "A Bug is a piece of code that knows whatz its purpose"

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                No worries. That makes more sense. It still doesn't work. I must have the MappingName messed up some how. Whether I do this programmatically or through the properties window, my changes don't take. 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