日期:2014-05-20  浏览次数:20499 次

如何让gridview中的checkbox根据数据库情况默认选中?
我有俩表 
table1 filed1 filed2
  1 22
  2 2323
  3 343
  4 2323
table2 filed1 filed2
  1 22
  3 343


通过 gridview 绑定 table1 和 checkbox 并且 让gridview中的checkbox 根据table2 默认选中(即table2中有的行默认选中)



------解决方案--------------------
在业务层用c#把这两个table处理成一个结果table

当然在结果table里要新增一个bool类型的字段

最后在前台用checkbox绑定此bool字段即可
------解决方案--------------------
探讨
在业务层用c#把这两个table处理成一个结果table

当然在结果table里要新增一个bool类型的字段

最后在前台用checkbox绑定此bool字段即可

------解决方案--------------------
探讨
在业务层用c#把这两个table处理成一个结果table

当然在结果table里要新增一个bool类型的字段

最后在前台用checkbox绑定此bool字段即可

------解决方案--------------------
探讨
在业务层用c#把这两个table处理成一个结果table

当然在结果table里要新增一个bool类型的字段

最后在前台用checkbox绑定此bool字段即可

------解决方案--------------------
学习
------解决方案--------------------
在Gridview的数据绑定的事件中,在数据加载完循环一次Gridview中的checkbox列,把这个里面的checkbox的checked设置成true
------解决方案--------------------
up
------解决方案--------------------
你是这个意思么?
select * from table1 where filed1 in (select filed1 from table2)
然后在你的业务层做下判断,有就true,没有就fales,来设置checkbox是否被选中
------解决方案--------------------
rowsdatabound中一行行处理。
------解决方案--------------------
在这个事件里写代码
C# code
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";

    }

  }

------解决方案--------------------
datalist控件的,可以作为参考
C# code
protected void dlCcList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (Request["mailerId"] == null)
                return;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string mailerId = Request["mailerId"];
                Ecommerce.BLL.Ecommerce_Cclist_Mailer cclist_MailerBll = new Ecommerce.BLL.Ecommerce_Cclist_Mailer();
                string dataKey = dlCcList.DataKeys[e.Item.ItemIndex].ToString();
                List<Ecommerce.Model.Ecommerce_Cclist_Mailer> cclist_MailerList
                            = cclist_MailerBll.GetModelList("mailerid = " + mailerId + " and cclistid=" + dataKey);

                if (cclist_MailerList.Count > 0)
                {
                    ((CheckBox)e.Item.FindControl("CheckBox1")).Checked = true;
                }
            }
        }

------解决方案--------------------
有默认属性的 你只要checkbox 的 value里面字就可以设置
------解决方案--------------------
C# code

DataSet ds=new DataSet();
        using(SqlConnection conn=new SqlConnection("server=(local);database=你的数据库;integrated security=sspi"))
        {
            SqlDataAdapter da=new SqlDataAdapter("select * from table1",conn);
            da.Fill(ds,"table1");
            DataColumn filed3 = new DataColumn("isExist", typeof(System.Boolean));
            ds.Tables["table1"].Columns.Add(filed3);
            for (int i = 0; i < ds.Tables["table1"].Rows.Count; i++)
            {
                SqlCommand cmd=new SqlCommand(string.Format("select * from table2 where filed1='{0}'",ds.Tables["table1"].Rows[i][0]),conn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow);
                if (dr.Read())
                {
                    ds.Tables["table1"].Rows[i][2] = true;
                }
                else
                {
                    ds.Tables["table1"].Rows[i][2] = false;
                }
            }
        }