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. C# Referencing Cell on DataGrid

C# Referencing Cell on DataGrid

Scheduled Pinned Locked Moved C#
csharpdatabasevisual-studioquestion
7 Posts 4 Posters 1 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.
  • W Offline
    W Offline
    Wheels012
    wrote on last edited by
    #1

    Good afternoon. I am trying to reference the cell on a datagrid. The Description column is hidden, but I want the description to populate a text box when the user clicks on a specific row. I have the following so far: rtxtDescription.Text = DataGridView1.Rows[index].Cells[2].Value.ToString(); The column is 2 and the row would depended on which row is selected. I am using Visual Studio 2008. Any suggestions? WHEELS

    D 1 Reply Last reply
    0
    • W Wheels012

      Good afternoon. I am trying to reference the cell on a datagrid. The Description column is hidden, but I want the description to populate a text box when the user clicks on a specific row. I have the following so far: rtxtDescription.Text = DataGridView1.Rows[index].Cells[2].Value.ToString(); The column is 2 and the row would depended on which row is selected. I am using Visual Studio 2008. Any suggestions? WHEELS

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      CellClick event?

      Time is the best teacher; unfortunately it kills all of its students. जय हिंद

      W 1 Reply Last reply
      0
      • D dan sh

        CellClick event?

        Time is the best teacher; unfortunately it kills all of its students. जय हिंद

        W Offline
        W Offline
        Wheels012
        wrote on last edited by
        #3

        It is actually in a method: public void PopulateDataGrid() { ds = new DataSet(); ds = d.FillDataGrid(this.cboDepartment.SelectedValue.ToString(), this.txtAcronym.Text); DataGridView1.Columns.Clear(); if (ds != null && ds.Tables.Count != 0) { BindingSource bs = new BindingSource(); bs.DataSource = ds; bs.DataMember = ds.Tables[0].TableName; DataGridView1.DataSource = bs; //return; } foreach (DataGridViewColumn col in DataGridView1.Columns) { if (col.HeaderText == constrDescrColumn) { col.Visible = false; } } //MessageBox.Show(DataGridView1.CurrentRow.Cells.IndexOf(.ToString()); int index = 0; rtxtDescription.Text = DataGridView1.Rows[index].Cells[2].Value.ToString(); }

        L 1 Reply Last reply
        0
        • W Wheels012

          It is actually in a method: public void PopulateDataGrid() { ds = new DataSet(); ds = d.FillDataGrid(this.cboDepartment.SelectedValue.ToString(), this.txtAcronym.Text); DataGridView1.Columns.Clear(); if (ds != null && ds.Tables.Count != 0) { BindingSource bs = new BindingSource(); bs.DataSource = ds; bs.DataMember = ds.Tables[0].TableName; DataGridView1.DataSource = bs; //return; } foreach (DataGridViewColumn col in DataGridView1.Columns) { if (col.HeaderText == constrDescrColumn) { col.Visible = false; } } //MessageBox.Show(DataGridView1.CurrentRow.Cells.IndexOf(.ToString()); int index = 0; rtxtDescription.Text = DataGridView1.Rows[index].Cells[2].Value.ToString(); }

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Wheels012 wrote:

          It is actually in a method:

          In your first post you asked for suggestions and the reply was "CellClick event" Now I could be wrong, but if I am correct it appears you have completely missed the point.

          W 1 Reply Last reply
          0
          • L led mike

            Wheels012 wrote:

            It is actually in a method:

            In your first post you asked for suggestions and the reply was "CellClick event" Now I could be wrong, but if I am correct it appears you have completely missed the point.

            W Offline
            W Offline
            Wheels012
            wrote on last edited by
            #5

            Good afernoon led mike. I am very new to C# (huge VBA background). My first hurdle is to populate the text box with the discription from the first cell from the description column. Secondly I need to add this to the click event once a user changes from record to record. WHEELS

            S 1 Reply Last reply
            0
            • W Wheels012

              Good afernoon led mike. I am very new to C# (huge VBA background). My first hurdle is to populate the text box with the discription from the first cell from the description column. Secondly I need to add this to the click event once a user changes from record to record. WHEELS

              S Offline
              S Offline
              Samuel Cherinet
              wrote on last edited by
              #6

              you can use the indexer of the datagridveiw to directly find the cell you are looking for and also use the property "FormattedValue" to get the displayed text of the cell

              str=dgrid[colIndex,rowIndex].FormattedValue.ToString();

              I have to remind you,If you already don't know, that an empty cell means most of the time null, that is like a fuel to run time error, so be aware of that. - try using the "RowEnter" event and access the selected row by the "DataGridViewCellEventArgs" in the event handler. best of luck

              W 1 Reply Last reply
              0
              • S Samuel Cherinet

                you can use the indexer of the datagridveiw to directly find the cell you are looking for and also use the property "FormattedValue" to get the displayed text of the cell

                str=dgrid[colIndex,rowIndex].FormattedValue.ToString();

                I have to remind you,If you already don't know, that an empty cell means most of the time null, that is like a fuel to run time error, so be aware of that. - try using the "RowEnter" event and access the selected row by the "DataGridViewCellEventArgs" in the event handler. best of luck

                W Offline
                W Offline
                Wheels012
                wrote on last edited by
                #7

                Thank you everyone. This is a great forum. WHEELS

                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