Click here to Skip to main content
15,914,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have used a one data gridview and two text box and two button in a form of windows application, one button for Save and other one for Update.After Save the datas, it will be display in data gridview. and then when i click the cell of datagridview,the data will display in the text box by using selectionchanged event.

C#
private void dgvService_SelectionChanged(object sender, EventArgs e)
       {
           txtService.Text = dgvService.CurrentCell.Value.ToString();
           label2.Text = dgvService.CurrentCell.Value.ToString();
       }



By using the above example, i can display only one column.But when i click one row, the first column of data gridview should be display in first text box and second column of data gridview should display in second text box.

For This question,kindly send me the answer.


Thanks,

Viswanathan.M
Posted
Updated 13-Jul-11 3:25am
v2
Comments
vino2012 13-Jul-11 9:19am    
I think you have to go through a topic "Grid View Field Control".Using that concept only you are able to retrieve or edit or delete a data from 'x' row and 'y' column of your gridview.

Handle the DataGridView.SelectionChanged event:
private void myDataGridView_SelectionChanged(object sender, EventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null && dgv.SelectedRows.Count > 0)
        {
        DataGridViewRow row = dgv.SelectedRows[0];
        if (row != null)
            {
            myFirstTextBox.Text = row.Cells[0].Value.ToString();
            mySecondTextBox.Text = row.Cells[1].Value.ToString();
            }
        }
    }
You may want to set the DataGridView.SelectionMode to DataGridViewSelectionMode.FullRowSelect
 
Share this answer
 
Comments
Viswanthan.M. 13-Jul-11 9:40am    
Really Thank You Mr.....I got it..
OriginalGriff 13-Jul-11 9:45am    
You're welcome!
Try
C#
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
           
                textbox1.Text = dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value.ToString();
                textbox2.Text =    dataGridView1.Rows[e.RowIndex].Cells["Column2"].Value.ToString();

          
            
        }
 
Share this answer
 
Comments
Viswanthan.M. 13-Jul-11 9:41am    
Ya...Its working...Thank you..
RaviRanjanKr 13-Jul-11 10:10am    
You are Welcome :)
RaviRanjanKr 13-Jul-11 10:11am    
if it helped you so You can mark it as Accept Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900