I was working on C# windows application as front end and SQL as back end I have display my sql table data in dtatgridview.

I want to display the data in textbox whatever row i select in datagridview.

I have write some code but its not showing data in textboxes.

here is my code:

the code is on datagridview cellcontentclick event

cn.ConnectionString = "Data Source=sam-AB59A9C19;Initial Catalog=master;Integrated Security=True";
            cn.Open();


            SqlCommand cmd = new SqlCommand();
            SqlDataReader rdr;
            string cd = dataGridView1.SelectedRows.ToString();
            string CommandText = "select * from acc where id=@id";

            cmd = new SqlCommand(CommandText);
            cmd.Connection = cn;
            cmd.Parameters.Add(new SqlParameter("@id", System.Data.SqlDbType.VarChar, 20, "id"));
            cmd.Parameters["@id"].Value = dataGridView1.SelectedRows.ToString();
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                textBox1.Text = rdr["id"].ToString();
                textBox2.Text = rdr["sub"].ToString();   
                textBox3.Text = rdr["marks"].ToString();
            }
            cn.Close();

the code does not display any error message.

hi

If you want the values of the selected rows of the content in the textbox then you can directly write this code on Datagridview_Cell Click
Textbox1.Text=Results.Rows[e.RowIndex].Cells["your Cloumn Name"].Value.Tostring();

how to do this with entire row selection.

Results.Rows[e.RowIndex].Cells["your Cloumn Name"].Value.Tostring();

this line stends for
e.RowIndex= your selected rows and
Cells["your Cloumn Name"].Value= your column name which you have selecetd Row
So put the Code in gidview_RowEnter Event .

YOu cannot read a whole row of the dgv to a string. Ok, you can, but there will be no value you want to have. So you have to specify from which column and from which row you want to get the value.

Lets repair your code:

//just make sure you initialize SqlConnection!!
            cn.ConnectionString = "Data Source=sam-AB59A9C19;Initial Catalog=master;Integrated Security=True";                                  
            string cd = dataGridView1[0, e.RowIndex].Value.ToString(); //0 represens the 1st column 
            //!!! change the number to the column you want to get the correct value !!!

            string CommandText = "select * from acc where id = @id";
            SqlCommand cmd = new SqlCommand(CommandText, cn);
            cmd.Connection = cn;
            cmd.Parameters.Add("@id", System.Data.SqlDbType.VarChar, 20).Value = cd;
            cn.Open();  
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                textBox1.Text = (string)rdr[0];
                textBox2.Text = (string)rdr[1];   
                textBox3.Text = (string)rdr[2];
            }
            cn.Close();

This should work.

Use This code this will pick the contents of the current cell and place in the textBox

{
//rest of the code
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellClick);
//rest of the code
}

 void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
            {
                textBox1.Text=dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            }

and if you want the whole record then you can use more than one textbox to display them like

void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
            {
                textBox1.Text=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
textBox2.Text=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
//.....
texBoxn.Text=dataGridView1.Rows[e.RowIndex].Cells[n+1].Value.ToString();

            }

virusisfound, Have you salved the problem regarding this thread?

Hi if i want to take strings var from each columns in my datagridview and use them in another form in the same project how can i achieve that?

for example

Method1(Something1, Something2);

void dataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
            {
string Something1=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
string Something2=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
           }

Method1 is in the same WFA or in another

I m new on programming!

Comment pourrai-je faire appel à une autre fenetre sur C sharp?

how to call on a window on Csharp

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex == 0)

            string LoginId = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            string Name = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            string EmailId = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            string Address = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
            string url = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
            var path = Application.StartupPath.Substring(0, (Application.StartupPath.Length - 10));
            textBox2.Text = Name;
            textBox3.Text = EmailId;
            textBox4.Text = Address;
            Bitmap image = new Bitmap(path + url);
            imagetxt.Image = image;
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.