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. DataGrid, 2 (easy) Questions

DataGrid, 2 (easy) Questions

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 4 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.
  • D Offline
    D Offline
    DougW48
    wrote on last edited by
    #1

    I have 2 questions concerning a DataGrid. Basically, I have a DataTable with 8 DataColumns. The table is initially empty, and the user fills in the information at run-time. Here are my questions: 1) I don't want every column being the same width at startup. How can I programatically change the column widths directly after I set the DataSource? 2) Each time a new row is created in my table, all of the cells contain "(null)". I want the cells to just be initially empty. How is this done? These will probably be 2 very easy questions to answer. I appreciate the help! :)

    S C P 3 Replies Last reply
    0
    • D DougW48

      I have 2 questions concerning a DataGrid. Basically, I have a DataTable with 8 DataColumns. The table is initially empty, and the user fills in the information at run-time. Here are my questions: 1) I don't want every column being the same width at startup. How can I programatically change the column widths directly after I set the DataSource? 2) Each time a new row is created in my table, all of the cells contain "(null)". I want the cells to just be initially empty. How is this done? These will probably be 2 very easy questions to answer. I appreciate the help! :)

      S Offline
      S Offline
      SapiensBwG
      wrote on last edited by
      #2

      i cant answer the first one! u cant make empty variables! when u create them and do not assign them a value they are null. u can try assinging them an empty string like code>string empty = "";<

      D 1 Reply Last reply
      0
      • S SapiensBwG

        i cant answer the first one! u cant make empty variables! when u create them and do not assign them a value they are null. u can try assinging them an empty string like code>string empty = "";<

        D Offline
        D Offline
        DougW48
        wrote on last edited by
        #3

        Assigning empty strings worked just fine. Thank you for your post! :-D

        1 Reply Last reply
        0
        • D DougW48

          I have 2 questions concerning a DataGrid. Basically, I have a DataTable with 8 DataColumns. The table is initially empty, and the user fills in the information at run-time. Here are my questions: 1) I don't want every column being the same width at startup. How can I programatically change the column widths directly after I set the DataSource? 2) Each time a new row is created in my table, all of the cells contain "(null)". I want the cells to just be initially empty. How is this done? These will probably be 2 very easy questions to answer. I appreciate the help! :)

          C Offline
          C Offline
          Charlie Williams
          wrote on last edited by
          #4

          The answer to both questions is to apply a DataGridTableStyle.

          DataGridTableStyle style = new DataGridTableStyle();
          style.MappingName = "MyTableName"; // Name of the DataTable
          DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
          colStyle.Width = 85;
          colStyle.NullText = "";
          colStyle.MappingName = "Column1"; // Name of the column in the DataTable
          colStyle.HeaderText = "First Name"; // This will be displayed as the column header
          style.GridColumnStyles.Add(colStyle);
          dataGrid1.TableStyles.Add(style);

          Charlie if(!curlies){ return; }

          1 Reply Last reply
          0
          • D DougW48

            I have 2 questions concerning a DataGrid. Basically, I have a DataTable with 8 DataColumns. The table is initially empty, and the user fills in the information at run-time. Here are my questions: 1) I don't want every column being the same width at startup. How can I programatically change the column widths directly after I set the DataSource? 2) Each time a new row is created in my table, all of the cells contain "(null)". I want the cells to just be initially empty. How is this done? These will probably be 2 very easy questions to answer. I appreciate the help! :)

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

            As an answer to your first question: you can add DataGridStyles to your Grid, in the designer and in code. Do this before you point the Grid to its datasource. If you program the available columns manually, you can set their Width and headertext too. All columns you want invisible, set the respective widths to 0. You can also define a methodthat returns a DataGridStyle and add that to the Grids style, possibly deleting the old style (DataSource has to be null to do that I believe)

            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