0

I used datagrid to show sql table datagrid.

And I showed one column on datagrid from sql.

Now I want to determine that which rows is now open & what is that values.

Like this.

See what I want to do.

And I used this code to show datatable on my datagrid.

string ConString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConString))
{

    SqlCommand cmd = new SqlCommand("SELECT roll FROM cmt_7th WHERE name IS Null And department IS Null And phone IS Null", con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable("cmt_7th");
    sda.Fill(dt);
    MydataGrid_roll.ItemsSource = dt.DefaultView;

}

And its show only row column. But I want to declare like my previous image.

SabbirTT
  • 131
  • 1
  • 2
  • 8

2 Answers2

0

You can get the selected row value with this code: string em = dataGridView1.CurrentRow.Cells[2].FormattedValue.ToString(); You store the value in a string called em like the above, you can change the name for sure, and then cell[] is like an array, it stores each intersection of a row and a column in there, so for instance if you have 3 columns and two rows, the first intersection of the row and column is of index 0 and so on and so forth.

0

You could select all values that you are interested in up front of but only display the "roll" column in the DataGrid:

string ConString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConString))
{

    SqlCommand cmd = new SqlCommand("SELECT roll, tc, tf, pc, pf FROM cmt_7th WHERE name IS Null And department IS Null And phone IS Null", con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable("cmt_7th");
    sda.Fill(dt);
    MydataGrid_roll.AutoGenerateColumns = false;
    MydataGrid_roll.Columns.Add(new DataGridTextColumn() { Header = "roll", Binding = new Binding("roll") });
    MydataGrid_roll.ItemsSource = dt.DefaultView;
}

You can then retrive any column values of the selected row in the DataGrid like this:

DataRowView drv = MydataGrid_roll.SelectedItem as DataRowView;
if(drv != null)
{
    int selectedRoll = Convert.ToInt32(drv["roll"]);
    if(drv["tc"] != DBNull.Value)
    {
        string selectedTc = drv["tc"].ToString();
    }
    //...
}
mm8
  • 163,881
  • 10
  • 57
  • 88