how to get cell value from datagridview?

Answered how to get cell value from datagridview?

  • Friday, January 11, 2008 10:45 PM
     
     
    Hi everyone,

    I have got one table with 4 columns. The first column is the "id column" with primary keys. I have to obtain the value of the cell which is in first column and in selected row by clicking a simple button. The value should appear in textbox.

    It is no problem for me to do it with dataGridView_RowEnter event

    Code Block

                textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells["COLUMN_ID"].FormattedValue.ToString();


    So how to obtain the cell value of the selected row in column "COLUMN_ID" by clicking a simple button?

    Thanks in advance


All Replies

  • Monday, January 14, 2008 8:43 AM
     
     Answered

    use this

    where DataGridView1 is your datagridview name'

     

    string str;

    str = DataGridView1.Rows[DataGridView.SelectedRows[0].Index].Cells[X].Value.ToString();

     

    where X is the column you want to read.

     

    Note:  it suppose to be possible to retrieve it like this, i try but didn't work:

     

    str = DataGridView.SelectedRows[0].Cells[X].Value.ToString();

     

    where 0 let you read from multiple selectedrows.  0 is the first selectedrows, 1 the second and so on...

    this can be useful if you want to read or manipulate multiple selectedrows.

     

    hope this help

  • Monday, January 14, 2008 12:16 PM
     
     
    HI all. Sorry the intromission, but i have a problem related with what youre discussing here.

    Im trying to write to a xml values from a datagridview using something like:

    myXmlTextWriter.WriteString(datagridview1.Rows[0].Cells["Something"].FormatedValue.ToString());

    my problem is when i try to write multiple times using a similar code to the same xml, it wont write anything at all...

    thanks in advance
  • Monday, January 14, 2008 10:52 PM
     
     

    have you try the example i write above???

     

    can you get just the datagridview value to a simple string, like i write in my example???

     

  • Monday, January 14, 2008 11:04 PM
     
     

    nevermind... the error was mine, of course Wink

    I misstyped the column name... Tongue Tied and with that, the whole xml got corrupted... lol

    Sorry about the inconvenience.

    NeoBugs, thanks anyway, your code was really helpfull!!

  • Tuesday, October 30, 2012 11:46 AM
     
     

    Heres a good way of obtaining values from desired cells ;

    I obtain certain values from a selected  row after I click on the cell,

    my example is where I click on one cell , the event fires the following:

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
               
                     string PinInGrid=PincodedataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    nameTextbox.Text = PinInGrid;
                }