none
I want to fill in a text box from a Selected Column in a GridView. RRS feed

  • Question

  •  

    Could someone kindly tell me how I would fill a textBox with a Cell that has been selected from a gridView?  I have tried and cannot figure out what option I want? 

     

    I was trying to do it like this:

     

    private void memberDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

    {

    txtBoxHouseholdName.Text = memberDataGridView1.???????

    }

     

    I cannot figure out what option I need here?

     

    Thanks!

    Tuesday, September 11, 2007 5:25 PM

All replies

  • In very generic terms you should be able to do this:

     

    Code Snippet

    private void memberDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

    {

       txtBoxHouseholdName.Text = memberDataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

    }

     

     

    What you'll want to keep in mind though is this:

    • In order for CellContentClick to fire, you must click right on the text/data.  In otherwords, if your column 200 pixels wide, and your data takes up the first 20 pixels, the CellContentClick event doesn't fire if you click the 180 pixels that don't have any data in it (this appears to be true for cells that are based on a textbox, I didn't test it against dropdown listboxes or anything else).  If you want the data in the cell, no matter where in the cell they click, then switch to the CellClick event instead.
    • You'll probably want to make sure the specified row and column are valid, so I'd suggest making your code look like this to be on the safe side:

    Code Snippet

    private void memberDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

    {

       if( (e.ColumnIndex > -1) && (e.RowIndex > -1) )

          txtBoxHouseholdName.Text = memberDataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

    }

     

     

    Wednesday, September 12, 2007 2:04 PM
  • That is incredible HELP!!! Thank you so much!!!

     

     

     

    Friday, September 14, 2007 2:13 PM