简体   繁体   中英

SelectedRow Error DataGridView Error

I want to get the value from the column when the row is selected. So I tried this code :

private void button1_Click(object sender, EventArgs e)
{
     textBox1.Text = dataGridView1.SelectedRow.Cells[0].Text;
}

But at the SelectedRow it showing this error :

"Are you missing any directive or assembly reference"

I'm using the two namespaces

using System.Web;
using System.Web.UI.WebControls; 

You should try this.

textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Text;

OR

textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value;

As it seems, you are working with windows forms platform, you need to use System.Windows.Forms namespace which can be found in System.Windows.Forms.dll assembly :

Importing namespace will be like this :

using System.Windows.Forms;

Your code itself need to make correction. If you want to use SelectedRows, then your code should be:

dataGridView1.SelectedRows[0].Cells[0].Value

And you need to change cell value to string to set in textbox Text property by doing :

textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

I understood what you want.Try this code:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
               int i = e.RowIndex;
                DataGridViewRow row = dataGridView1.Rows[i];
                textBox1.Text = row.Cells[0].Value.ToString();
                textBox2.Text = row.Cells[2].Value.ToString();    
            }

From this code when you double click on the any row cells you get that row column values.Hope this solve your problem:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM