Quick check to see if Columnname exists in a datagridview

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RSH

    Quick check to see if Columnname exists in a datagridview

    Hi,

    I have a situation where I have a dynamically loaded DataGridView and I need
    to see if a particular column exists in the DataGridView before I enable it
    for editing. Is there an easy way to see if the column exists?

    Thanks!
    Ron


  • RSH

    #2
    Re: Quick check to see if Columnname exists in a datagridview

    Silly me!

    In case anyone else was wondering...

    if (dataGridView1. Columns["Modified"] != null)

    {

    dataGridView1.C olumns["Modified"].ReadOnly = true;

    }





    "RSH" <way_beyond_oop s@yahoo.com> wrote in message
    news:%23zVgbXFT GHA.4608@tk2msf tngp13.phx.gbl. ..[color=blue]
    > Hi,
    >
    > I have a situation where I have a dynamically loaded DataGridView and I
    > need to see if a particular column exists in the DataGridView before I
    > enable it for editing. Is there an easy way to see if the column exists?
    >
    > Thanks!
    > Ron
    >[/color]


    Comment

    • Lars-Inge Tønnessen \(VJ# MVP\)

      #3
      Re: Quick check to see if Columnname exists in a datagridview

      Please use the "Columns.Contai ns("column name")" method.
      Example:

      Lets say we have a DataGrid called "dataGridView1" .

      this.dataGridVi ew1.Columns.Add ("One", "One");

      if (this.dataGridV iew1.Columns.Co ntains("One"))
      System.Windows. Forms.MessageBo x.Show("Yes");

      this.dataGridVi ew1.Columns.Add ("Two", "Two");
      this.dataGridVi ew1.Columns.Add ("Three", "Three");


      Regards,
      Lars-Inge Tønnessen
      Siemens Medical - Norway


      Comment

      • Lars-Inge Tønnessen \(VJ# MVP\)

        #4
        Re: Quick check to see if Columnname exists in a datagridview

        Please use "Contains". :o)

        Regards,
        Lars-Inge Tønnessen
        Siemens Medical - Norway


        Comment

        • RSH

          #5
          Re: Quick check to see if Columnname exists in a datagridview

          What is the difference? This appears to work as is outright.


          "Lars-Inge Tønnessen (VJ# MVP)" <http://emailme.larsing e.com> wrote in
          message news:u$QRuAGTGH A.1576@tk2msftn gp13.phx.gbl...[color=blue]
          > Please use the "Columns.Contai ns("column name")" method.
          > Example:
          >
          > Lets say we have a DataGrid called "dataGridView1" .
          >
          > this.dataGridVi ew1.Columns.Add ("One", "One");
          >
          > if (this.dataGridV iew1.Columns.Co ntains("One"))
          > System.Windows. Forms.MessageBo x.Show("Yes");
          >
          > this.dataGridVi ew1.Columns.Add ("Two", "Two");
          > this.dataGridVi ew1.Columns.Add ("Three", "Three");
          >
          >
          > Regards,
          > Lars-Inge Tønnessen
          > Siemens Medical - Norway
          >
          >[/color]


          Comment

          • Demetri

            #6
            Re: Quick check to see if Columnname exists in a datagridview

            1. Its a more reliable method of determining if a column exists. By your
            method what if you port your code to .Net 2.x and in the new version it does
            not return null but instead it returns an empty column?

            2. Its just better coding practice. It begs the question, why did Microsoft
            bother giving you a Contains method? Think about it.

            3. It also explicitly states your intentions, which is probably the most
            important factor of all.

            --
            -Demetri


            "RSH" wrote:
            [color=blue]
            > What is the difference? This appears to work as is outright.
            >
            >
            > "Lars-Inge Tønnessen (VJ# MVP)" <http://emailme.larsing e.com> wrote in
            > message news:u$QRuAGTGH A.1576@tk2msftn gp13.phx.gbl...[color=green]
            > > Please use the "Columns.Contai ns("column name")" method.
            > > Example:
            > >
            > > Lets say we have a DataGrid called "dataGridView1" .
            > >
            > > this.dataGridVi ew1.Columns.Add ("One", "One");
            > >
            > > if (this.dataGridV iew1.Columns.Co ntains("One"))
            > > System.Windows. Forms.MessageBo x.Show("Yes");
            > >
            > > this.dataGridVi ew1.Columns.Add ("Two", "Two");
            > > this.dataGridVi ew1.Columns.Add ("Three", "Three");
            > >
            > >
            > > Regards,
            > > Lars-Inge Tønnessen
            > > Siemens Medical - Norway
            > >
            > >[/color]
            >
            >
            >[/color]

            Comment

            • Lars-Inge Tønnessen \(VJ# MVP\)

              #7
              Re: Quick check to see if Columnname exists in a datagridview

              The Microsoft developers always use this method when they are writing code.
              I guess it has something to do with forward compatibility and stability.

              Regards,
              Lars-Inge Tønnessen


              Comment

              • RSH

                #8
                Re: Quick check to see if Columnname exists in a datagridview

                Fair enough.

                Thank you.

                Ron

                "Lars-Inge Tønnessen (VJ# MVP)" <http://emailme.larsing e.com> wrote in
                message news:OlzuFiGTGH A.224@TK2MSFTNG P10.phx.gbl...[color=blue]
                > The Microsoft developers always use this method when they are writing
                > code. I guess it has something to do with forward compatibility and
                > stability.
                >
                > Regards,
                > Lars-Inge Tønnessen
                >
                >[/color]


                Comment

                Working...