Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the Values of Selected Row from a Gridview in TextBox using object Datasource
Posted

Use the following Code :
C#
void Button1_Click(Object sender, EventArgs e)
 {

   // Get the currently selected row using the SelectedRow property.
   GridViewRow row = CustomersGridView.SelectedRow;

   // In this example, the first column (index 0) contains
   TextBox1.Text = row.Cells[0].Text;

 }
 
Share this answer
 
Comments
thatraja 25-Nov-11 5:04am    
5!
Manoj K Bhoir 25-Nov-11 10:16am    
Thanx
Try

C#
// get the values of selected row from a Gridview and display the values in textboxes using C# code.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  {
      TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
     
  }


This How to get the Values of Selected Row from a Gridview in TextBox using ASP.NET[^] article is good to learn how to get selected row values from a GridView.
 
Share this answer
 
v2
Comments
thatraja 25-Nov-11 5:04am    
5!
RaviRanjanKr 25-Nov-11 7:23am    
Thanks :)
 
Share this answer
 
try this...

1.select click event of datagridview
2.find row index like...
C#
int row = dgvReleaseOrder.CurrentCell.RowIndex;

3.using for loop
C#
for (int row = 0; row < dgvReleaseOrder.Rows.Count - 1; row++)
               {
                   txt1.text = dgvReleaseOrder.Rows[row].Cells[1].Value.ToString();
                   txt2.text = dgvReleaseOrder.Rows[row].Cells[2].Value.ToString();
                   txt3.text = dgvReleaseOrder.Rows[row].Cells[3].Value.ToString();
                   txt4.text = dgvReleaseOrder.Rows[row].Cells[4].Value.ToString();
                   txt5.text= dgvReleaseOrder.Rows[row].Cells[5].Value.ToString();
                   txt6.text = dgvReleaseOrder.Rows[row].Cells[6].Value.ToString();
                   txt7.text= dgvReleaseOrder.Rows[row].Cells[7].Value.ToString();
                   txt8.text = dgvReleaseOrder.Rows[row].Cells[8].Value.ToString();
                   txt9.text= dgvReleaseOrder.Rows[row].Cells[9].Value.ToString();
                   txt10.text= dgvReleaseOrder.Rows[row].Cells[10].Value.ToString();

               }
 
Share this answer
 
Comments
CHill60 16-Dec-13 7:32am    
This question is 2 years old and this solution is essentially the same as the one you posted 2 days ago. It will still only result in the textboxes displaying the last row of the datagrid
 
Share this answer
 
Comments
CHill60 16-Dec-13 7:35am    
Solution at that link is the same as Solution 6 and Solution 7
C#
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Column-name-here"].Value.ToString();

        }
 
Share this answer
 
Comments
CHill60 14-Dec-13 7:54am    
The question is 2 years old!
Member 10848195 8-Sep-15 3:00am    
textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["Column-name-here"].Value.ToString();
Member 13982358 18-Sep-18 21:39pm    
this works great but I had to give a number in cells not a name like cells[0]
 
Share this answer
 
Comments
JoCodes 14-Dec-13 3:54am    
Rakesh , The question was posted by the OP 2 years back :)
i hope,this code helps you

C#
for (int row = 0; row < dgvReleaseOrder.Rows.Count - 1; row++)
               {
                   txt1.text = dgvReleaseOrder.Rows[row].Cells[1].Value.ToString();
                   txt2.text = dgvReleaseOrder.Rows[row].Cells[2].Value.ToString();
                   txt3.text = dgvReleaseOrder.Rows[row].Cells[3].Value.ToString();
                   txt4.text = dgvReleaseOrder.Rows[row].Cells[4].Value.ToString();
                   txt5.text= dgvReleaseOrder.Rows[row].Cells[5].Value.ToString();
                   txt6.text = dgvReleaseOrder.Rows[row].Cells[6].Value.ToString();
                   txt7.text= dgvReleaseOrder.Rows[row].Cells[7].Value.ToString();
                   txt8.text = dgvReleaseOrder.Rows[row].Cells[8].Value.ToString();
                   txt9.text= dgvReleaseOrder.Rows[row].Cells[9].Value.ToString();
                   txt10.text= dgvReleaseOrder.Rows[row].Cells[10].Value.ToString();

               }


happy coding
 
Share this answer
 
Comments
CHill60 14-Dec-13 7:56am    
The question is two years old, and this solution is not that different from Solution 3 already posted. Besides the text boxes here will only end up displaying the data from the last row

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