我想获取记录的四个字段的值,然后在sql语句中使用它们。当我单击gridview中的行/记录并将每个值存储在它们自己声明的变量中时,如何获取这4个值?
四个字段名称是course_abbreviation,course_name,month_of_admission和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)
}
答案 0 :(得分:0)
在GridView.RowClick事件中,有一个e.RowHandle参数,您可以使用该参数来标识单击的行。然后,使用GridView.GetRowCellValue方法获取该行的单元格值。
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]);
}
答案 1 :(得分:0)
为避免将列拖动到不同位置时出现问题,通常最好按名称引用。
object val1;
object val2;
object val3;
object val4;
private void gridView1_RowClick(object sender, RowClickEventArgs e) {
int rowHandle = e.RowHandle;
val1 = gridView1.GetRowCellValue(rowHandle, "course_abbreviation");
val2 = gridView1.GetRowCellValue(rowHandle, "course_name");
val3 = gridView1.GetRowCellValue(rowHandle, "month_of_admission");
val4 = gridView1.GetRowCellValue(rowHandle, "year_of_admission");
}