I want to get the values of four fields of a record and later use them in an sql statement. How can I get those 4 values when I click a row/record in gridview and have each value stored in their own declared variables?
The four field names are course_abbreviation, course_name, month_of_admission and year_of_admission.
public LecturerForm(string lecturer_firstname, string lecturer_lastname, string lecturer_id)
{
InitializeComponent();
string lecturer = lecturer_firstname + " " + lecturer_lastname;
lblname.Text = lecturer;
string lecID = lecturer_id;
string connetionString = null;
SqlConnection cnn;
connetionString = "Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=jms";
SqlDataAdapter sda = new SqlDataAdapter("Select unit_code, unit_name, course_abbreviation, course_name, group_name from units_allocation where national_id='" + lecID + "' ", connetionString);
cnn = new SqlConnection(connetionString);
DataTable dt4 = new System.Data.DataTable();
sda.Fill(dt4);
gridControl1.DataSource = dt4;
gridView1.Columns[0].Caption = "Unit Code";
gridView1.Columns[1].Caption = "Unit Name";
gridView1.Columns[2].Caption = "Course Abbr.";
gridView1.Columns[3].Caption = "Course Name";
gridView1.Columns[4].Caption = "Group";
}
private void gridView1_RowClick(object sender, RowClickEventArgs e)
{
//code to get values of four fields and store in four variables...(i.e from the question)
}
In the GridView.RowClick event there is the e.RowHandle argument which you can use to identify a clicked row. Then, use the GridView.GetRowCellValue method to get a cell value of that row.
object val1;
object val2;
object val3;
object val4;
private void gridView1_RowClick(object sender, RowClickEventArgs e) {
int rowHandle = e.RowHandle;
val1 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[0]);
val2 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[1]);
val3 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[2]);
val4 = gridView1.GetRowCellValue(rowHandle, gridView1.Columns[3]);
}