在 gridview 数据绑定后使用条件更改列值
Change column value using condition after gridview databind
我使用 join
在 Gridview 中显示来自三个表的数据。
但是我想使用条件更改字段。
我将 "status" 保留为“1”和“0”。
对于“1”,我想显示 "Selected",对于“0”,我想显示 "Rejected".
下面是我的代码:
void databind()
{
string CS = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("select a.applicant_id as 'Applicant ID',a.applicant_name as 'Name',a.status as 'Status',b.staff_Name as 'Reviewed by',c.course_name as 'Course' from tblApplicant a left outer join tblStaff b on a.staff_id=b.staff_id left outer join tblCourse c on a.course_id=c.course_id", con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
将此行与 case when
子句一起使用
SqlCommand cmd = new SqlCommand("select a.applicant_id as 'Applicant ID',a.applicant_name as 'Name',case when a.status=0 then 'Rejected' else 'Selected' end as 'Status',b.staff_Name as 'Reviewed by',c.course_name as 'Course' from tblApplicant a left outer join tblStaff b on a.staff_id=b.staff_id left outer join tblCourse c on a.course_id=c.course_id", con);
我使用 join
在 Gridview 中显示来自三个表的数据。
但是我想使用条件更改字段。
我将 "status" 保留为“1”和“0”。 对于“1”,我想显示 "Selected",对于“0”,我想显示 "Rejected".
下面是我的代码:
void databind()
{
string CS = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("select a.applicant_id as 'Applicant ID',a.applicant_name as 'Name',a.status as 'Status',b.staff_Name as 'Reviewed by',c.course_name as 'Course' from tblApplicant a left outer join tblStaff b on a.staff_id=b.staff_id left outer join tblCourse c on a.course_id=c.course_id", con);
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
将此行与 case when
子句一起使用
SqlCommand cmd = new SqlCommand("select a.applicant_id as 'Applicant ID',a.applicant_name as 'Name',case when a.status=0 then 'Rejected' else 'Selected' end as 'Status',b.staff_Name as 'Reviewed by',c.course_name as 'Course' from tblApplicant a left outer join tblStaff b on a.staff_id=b.staff_id left outer join tblCourse c on a.course_id=c.course_id", con);