Hi

I am using C# - Windows Application

wat my requirement is i have the gridview in my form

and also i bind the table to gridView

1 ) how to select a row in the gridview... wat the code... then wher shall i write the code..?

2) if i select the particualr row ... i want to pass the data on the selected row to the textbox...

Thank You..

Recommended Answers

All 5 Replies

Once you dragged and dropped the gridview on your form, it'd ask you about the datasource, just create datasource and let it the grid's datasource.
using some grid handlers you can handle on row selection something happen. first I'll let you create th grid and bind the data.
If you faced any problem reply to this thread..

Once you get all the stuff Ramy told you to do on your form, use the CellClick event of the grid to get at the data on that row.
You must check to be sure the user has selected a valid row. Row header, and Column headers return a value of -1 and therefore not a valid indexer.
This example expects a column named "MyData", however if you know the ordinal column you want to pull data from, then you can use that value.

private void MyGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
     if(e.RowIndex > -1)
        textBox1.Text = MyGrid.Rows[e.RowIndex].Cells["MyData"].Value.ToString();
}

Because you are a beginner, and we try to help beginners come up to speed fast on DaniWeb, I should point out that anytime you see an event handler that uses something other than the EventArgs type, it usually means it contains helpful data relevant to the event handler. In this case, the DataGridViewCellEventArgs type contains the Row and Column the user clicked.

Now in a full blown application, the author may want to hide some cells or not have them present at all in the grid, and you need to get at the bound data. In that case, you can still use the Row index to get to the underlying data in the Data table.

private void MyGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
     if(e.RowIndex > -1)
     {
            BindingSource bs = (BindingSource)MyGrid.DataSource;
            DataRowView drv = (DataRowView)bs.CurrencyManager.Current;
            textBox1.Text = drv[1].ToString();
     }
}

OR you can use a short-cut, which is harder to read, but performs the same action. Some of you viewers may or may not know you can do this is the only reason I am showing the following syntax: (I don't use this technique simply because of the readability issue). It is kind of cool that C# lets you double cast objects in this way.

private void MyGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
     if(e.RowIndex > -1)
            textBox1.Text = ((DataRowView)((BindingSource)MyGrid.DataSource).CurrencyManager.Current)[1].ToString();
}

The textBox1.Text is all on one line of code.
The only reason why you would want to use the binding source approach is if the grid does not contain the data you want, but it exists in the underlying table. There may even be associated/related tables in the dataset, that you may want to get at.

Have fun with C#,
Jerry

commented: Deserve to be Master poster :) +2

You're so helpful Jerrrrrrrry :)

Hi..
Let me share with u what i knw.. to select a single record in a gridview,while u r configuring the datasouce,specify the condition which should be applied to select that single row as for eg:select * from table where condition='" & str & "'..ok..
to read the data and to pass it to atext box, the code resembles the following..

con.Open()
cmd = New SqlCommand("Select * from ThirdParty where DestAcNo = '" & DropDownList1.Text & "'", con)
rdr = cmd.ExecuteReader()
While (rdr.Read())
TextBox1.Text = rdr("DestName").ToString()
TextBox2.Text = rdr("DestAcType").ToString()
TextBox4.Text = rdr("Amount").ToString()
'TextBox3.Text = rdr("Branch").ToString()
'TextBox7.Text = rdr("Phone").ToString()
'TextBox6.Text = rdr("Mob").ToString()
End While
con.Close()

1) How to select a single row knowing his index

//To select a single row knowing his index
myGridView.Rows[0].Selected = true;

2) How to select a single row knowing a value of one among his cells

//To select a single row knowing a value of one among his cells 
foreach (DataGridViewRow row in myGridView.Rows)
{
   if ((string)row.Cells[1].Value == /*A given value*/ "Ikura") row.Selected = true;

}

3) How to select a cell in a DataGridView programmatically knowing his index and his row index

//To select a cell in a DataGridView programmatically knowing his index and his row index
myGridView.Rows[0].Cells[0].Selected = true;

4) How to select a DataGridViewCell knwoing only his value

//To select a DataGridViewCell knwoing only his value
myGridView.Rows[0].Cells[0].Selected = false;
foreach (DataGridViewRow row in myGridView.Rows)
{
  foreach (DataGridViewCell cell in row.Cells)
  {
   if (Convert.ToString(cell.Value) == /*A given value*/"Ikura") cell.Selected = true;
  }
}

5) How to select a column Knowing his index

// To select a column Knowing his index 
int ColumnIndex = 2; // A given ColumnIndex
foreach(DataGridViewRow row in myGridView.Rows)
  foreach (DataGridViewCell cell in row.Cells)
       {
                    row.Cells[ColumnIndex].Selected = true;
       }

6) How to select a column Knowing his TextHeader

// To select a column Knowing his TextHeader
myGridView.Rows[0].Cells[0].Selected = false;
foreach (DataGridViewRow oRow in myGridView.Rows)
  foreach(DataGridViewColumn oColumn in myGridView.Columns)
       {
        if (oColumn.HeaderText == /*The TextHeader property here is*/"ProductName")
           {
             oRow.Cells[oColumn.Index].Selected = true;
           }
        }
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.