DataGridView Bug or Design Flaw?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?anAybXNmdA==?=

    DataGridView Bug or Design Flaw?

    I use unbound DataGridView controls in my form (simply because I have to
    manually message my data before populating the DataGridView with it).

    Say I clear all the info from the DataGridView:

    DataGridView1.C olumns.Clear()

    Now I manually add new columns to fill:

    DataGridView1.C olumns.Add("Dat e_Time", "Date_Time" )
    DataGridView1.C olumns.Add("Sta tion_ID", "Station_ID ")
    DataGridView1.C olumns.Add("Ope rator", "Operator")

    Now try to fill these columns using their Column Names used above:

    Dim dgRow As New DataGridViewRow ()
    dgRow.CreateCel ls(DataGridView 1)
    dgRow.Cells["Date_Time"].Value = DateTime.Now(). ToString()
    dgRow.Cells["Station_ID "].Value = "Test Station 1"
    dgRow.Cells["Operator"].Value = Form1.EmployeeN ameTextBox.Text
    DataGridView1.R ows.Add(dgRow);

    Here is the error:
    ArgumentExcepti on was unhandled
    Column named Date_Time cannot be found.
    Parameter name: columnName

    Is there a fix for this?

    Microsoft has overloaded this method to accept an integer Index or a string
    columnName, but the columnName version does not appear to work. My
    DataGridColumns are added Programmaticall y, so I don't always know what the
    Index value of the DataGridColumns are going to be.

    Is there a way to get the DataGridColumn' s Index using the columnName? Is
    this the way to get around this problem?

    I have also coded a different version using CellTemplates, but it threw the
    same error.
  • Patrice

    #2
    Re: DataGridView Bug or Design Flaw?

    dgdRow.SetValue s seems to work...

    This is where looking at the DataGridView code could be helpfull. I assume
    there is some method that triggers the actual creation.

    Is this fix enough or do you need a closer look ?

    --
    Patrice

    "jp2msft" <jp2msft@discus sions.microsoft .coma écrit dans le message de
    groupe de discussion : CEF3A977-C48D-42C1-8092-F039F33EA20A@mi crosoft.com...
    I use unbound DataGridView controls in my form (simply because I have to
    manually message my data before populating the DataGridView with it).
    >
    Say I clear all the info from the DataGridView:
    >
    DataGridView1.C olumns.Clear()
    >
    Now I manually add new columns to fill:
    >
    DataGridView1.C olumns.Add("Dat e_Time", "Date_Time" )
    DataGridView1.C olumns.Add("Sta tion_ID", "Station_ID ")
    DataGridView1.C olumns.Add("Ope rator", "Operator")
    >
    Now try to fill these columns using their Column Names used above:
    >
    Dim dgRow As New DataGridViewRow ()
    dgRow.CreateCel ls(DataGridView 1)
    dgRow.Cells["Date_Time"].Value = DateTime.Now(). ToString()
    dgRow.Cells["Station_ID "].Value = "Test Station 1"
    dgRow.Cells["Operator"].Value = Form1.EmployeeN ameTextBox.Text
    DataGridView1.R ows.Add(dgRow);
    >
    Here is the error:
    ArgumentExcepti on was unhandled
    Column named Date_Time cannot be found.
    Parameter name: columnName
    >
    Is there a fix for this?
    >
    Microsoft has overloaded this method to accept an integer Index or a
    string
    columnName, but the columnName version does not appear to work. My
    DataGridColumns are added Programmaticall y, so I don't always know what
    the
    Index value of the DataGridColumns are going to be.
    >
    Is there a way to get the DataGridColumn' s Index using the columnName? Is
    this the way to get around this problem?
    >
    I have also coded a different version using CellTemplates, but it threw
    the
    same error.

    Comment

    • =?Utf-8?B?anAybXNmdA==?=

      #3
      Re: DataGridView Bug or Design Flaw?

      I don't immediately see how this is going to solve this particular delima.

      When using SetValues(ByVal ParamArray values As Object()):

      How do I ensure that "06/05/2008" is going to be mapped to the "Date_Time"
      column if I do not know the index number of that column?

      In the DataGridView, there is the option to reorder the Columns. After they
      have been reordered, how would you go about constructing your ParamArray of
      values?

      "Patrice" wrote:
      dgdRow.SetValue s seems to work...
      >
      This is where looking at the DataGridView code could be helpfull. I assume
      there is some method that triggers the actual creation.
      >
      Is this fix enough or do you need a closer look ?
      >
      --
      Patrice
      >

      Comment

      • Rich P

        #4
        Re: DataGridView Bug or Design Flaw?

        The datagridview works best/easiest to manipulate when the datasource is
        based on a dataTable. So instead of adding columns to the datagridview
        you can add columns to the dataTable and massage your data there:

        Dim dt As DataTable
        dim col1 As DataColumn
        ...
        dt.oolumns.Add( col1)
        ...
        datagridview1.d atasource = dt '--this automatically create all the
        columns from the dataTable

        For information on creating dataTables from code and adding data there
        is good documentation in the VS2005 Help system.

        Rich

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • =?Utf-8?B?anAybXNmdA==?=

          #5
          Re: DataGridView Bug or Design Flaw?

          Hi Rich,

          That's a neat way to do it! And it simplifies life quite a bit.

          Your approach solves the programming task, but does not answer the question:
          Is there a bug or design flaw with accessing Cells by the column name?

          "Rich P" wrote:
          The datagridview works best/easiest to manipulate when the datasource is
          based on a dataTable. So instead of adding columns to the datagridview
          you can add columns to the dataTable and massage your data there:
          >
          Dim dt As DataTable
          dim col1 As DataColumn
          ...
          dt.oolumns.Add( col1)
          ...
          datagridview1.d atasource = dt '--this automatically create all the
          columns from the dataTable
          >
          For information on creating dataTables from code and adding data there
          is good documentation in the VS2005 Help system.
          >
          Rich
          >
          *** Sent via Developersdex http://www.developersdex.com ***
          >

          Comment

          Working...