views:

349

answers:

2

I'm trying to get my current program to take information from a dynamically created DataGridView. I have managed to get the information into the grid, and perform the required search, however now I'm really stuck.

I have added a column to the datagridview which holds a button within each row. What I'd like to do is take the value of the data from column index 1 which is in the same row as the button clicked. Confusing? Anyway, here's the code:

        public void GetValues(...)
    {


       //Details regarding connection, querying and inserting table
       .
       .
       .

        DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();
        buttonCol.Name = "ButtonColumn";
        buttonCol.HeaderText = "Select";
        buttonCol.Text = "Edit";
        //NB: the text won't show up on the button. Any help there either?

        dataGridView1.Columns.Add(buttonCol);
        dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            DataGridViewButtonCell button = (row.Cells["ButtonColumn"] as DataGridViewButtonCell);

        }
        dataGridView1.Columns["ButtonColumn"].DisplayIndex = 0;


    }


        void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Here is where I'm having the trouble. What do I put in here???
        }

Thanks for any help you can give!

David.

A: 

Your DataGridViewCellEventArgs contains very useful information such as RowIndex.

So something like (I don't know what you want to do with the value):

String dataYouWant = dataGridView1.Rows[e.RowIndex].Cells[1].Value;
Zwergner
Thanks very much, very helpful!
David Archer
A: 

`

 if (e.ColumnIndex != button_column_number) //column number of the button.
                    return;
                dataGridView1.EndEdit();
                bool val;
                if ((dataGridView1.Rows[e.RowIndex].Cells[1].Value) != null) // column index 1...as that's what you want.
                {
                   //d stuff you want here.
                }
                else
                {
                }

`

lune
Very in-depth - just wondering what the "Bool val" variable is there for?
David Archer