绑定数据时在gridview中使复选框为true [英] make checkbox true in gridview when binding data

查看:42
本文介绍了绑定数据时在gridview中使复选框为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gridview,并且有一列复选框,因此我想为来自数据库并与某些行数据匹配的值使复选框为true.那我该怎么办?

字符串s =从document_rights中选择document_id,user_id,其中user_id =""+ drop_user.SelectedItem.Value +"和category ="" + drop_cat.SelectedItem.Value +"和sub_cat =" + drop_sub_cat. SelectedItem.Value +''";
SqlDataAdapter广告=新的SqlDataAdapter(s,c.getcon());
DataSet ds = new DataSet();
ad.Fill(ds);

if(ds.Tables [0] .Rows.Count!= 0)
{
xxx = ds.Tables [0] .Rows [0] [0] .ToString();
string [] split = xxx.Split('','');
intArray1 =新的int [split.Length];
for(int i = 0; i< split.Length; i ++)
{
intArray1 [i] = System.Convert.ToInt32(split [i]);

}
字符串xxxx =从文档中id所在的位置选择标题,id"(;
for(int k = 0; k< intArray1.Length; k ++)
{
xxxx + = intArray1 [k] +,";
}
xxxx = xxxx.Substring(0,xxxx.Length-1)+)and category =''" + drop_cat.SelectedItem.Value +''和subctgry =""+ drop_sub_cat.SelectedItem.Value +"" ;
SqlDataAdapter ad1 =新的SqlDataAdapter(xxxx,c.getcon());
DataSet ds1 =新的DataSet();
ad1.Fill(ds1);

如果(ds1.Tables [0] .Rows.Count!= 0)
{
foreach(GridView1.Rows中的GridViewRow di)
{
CheckBox chkBx =(CheckBox)di.FindControl("CheckBox1");


}
}
GridView1.DataSource = c.getdata(从文档中选择id,标题,其中category =""+ drop_cat.SelectedItem.Text +"''和subctgry =" + drop_sub_cat.SelectedItem.Text +"));
GridView1.DataBind();
}

i am using gridview and there is a column of checkbox so i want to checkbox make true for value that come from database and match with some row data. so what can i do ?

string s = "select document_id,user_id from document_rights where user_id=''" +drop_user.SelectedItem.Value + "'' and category=''" +drop_cat.SelectedItem.Value+ "'' and sub_cat=''" + drop_sub_cat.SelectedItem.Value + "''";
SqlDataAdapter ad = new SqlDataAdapter(s, c.getcon());
DataSet ds = new DataSet();
ad.Fill(ds);

if (ds.Tables[0].Rows.Count != 0)
{
xxx = ds.Tables[0].Rows[0][0].ToString();
string[] split = xxx.Split('','');
intArray1 = new int[split.Length];
for (int i = 0; i < split.Length; i++)
{
intArray1[i] = System.Convert.ToInt32(split[i]);

}
string xxxx = "select title,id from document where id in(";
for (int k = 0; k < intArray1.Length; k++)
{
xxxx += intArray1[k] + ",";
}
xxxx = xxxx.Substring(0, xxxx.Length - 1) + ") and category=''" + drop_cat.SelectedItem.Value + "'' and subctgry=''" + drop_sub_cat.SelectedItem.Value + "''";
SqlDataAdapter ad1 = new SqlDataAdapter(xxxx, c.getcon());
DataSet ds1 = new DataSet();
ad1.Fill(ds1);

if (ds1.Tables[0].Rows.Count != 0)
{
foreach (GridViewRow di in GridView1.Rows)
{
CheckBox chkBx = (CheckBox)di.FindControl("CheckBox1");


}
}
GridView1.DataSource = c.getdata("select id,title from document where category=''" + drop_cat.SelectedItem.Text + "'' and subctgry=''" + drop_sub_cat.SelectedItem.Text + "''");
GridView1.DataBind();
}

推荐答案


我假设您想根据数据库中的值来选中一个CheckBox,然后,您可以使用如下代码:
Hi,
I presume you want to make a CheckBox checked based on a value from database, then, you can use something like this:
<asp:checkbox checked="<%# MakeChecked(Eval("FieldName")) %>" ...="" xmlns:asp="#unknown" />


然后在您的代码后面:


Then in your code behind:

public bool MakeChecked(object fieldValue)
{
   //Some code to evaluate fieldValue
   //and then based on that return true/false
}



干杯



Cheers


这篇关于绑定数据时在gridview中使复选框为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆