原帖地址http://topic.csdn.net/u/20100807/14/cfb015bd-9498-4ff1-9b58-33c75aeb27b4.html我在原来的基础上增加了一点功能现在遇到如下几个问题:(1)查询按钮需要点击两次,才能显示正确结果。
(2)如果点了重选(主要工作就是撤销Gridview的绑定,以及Checkboxlist选项清空),再次选择时,如果上次已选的项本次未选,,点击查询时,在“ GridView1.DataBind();”处会提示对应的字段找不到。这个原因应该是与1有关系。
重选代码:protected void Button2_Click(object sender, EventArgs e)
{
for (Int32 i = 0; i < CheckBoxList1.Items.Count; i++)
{
CheckBoxList1.Items[i].Selected = false;
}
GridView1.DataSource = null;
GridView1.DataBind();
GridView1.Visible = false; }查询代码:
protected void Button1_Click(object sender, EventArgs e)
{
string ConStr = "server = .\\sqlexpress;user id=sa;pwd=sa;database = gridview";
SqlConnection con = new SqlConnection(ConStr); String sqlStr = "";
for (Int32 i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
sqlStr += CheckBoxList1.Items[i].Value + ",";
}
}
sqlStr = sqlStr.Trim(','); String sql = "Select " + sqlStr + " from userinfo";
SqlDataAdapter ada = new SqlDataAdapter(sql, con);
con.Open(); DataSet ds = new DataSet();
ada.Fill(ds, "tt");
GridView1.Visible = true;
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.Columns.Clear(); for (Int32 i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{ BoundField col = new BoundField();
col.HeaderText = CheckBoxList1.Items[i].Text;
col.DataField = CheckBoxList1.Items[i].Value;
col.Visible = true;
GridView1.Columns.Add(col);
}
} con.Close();
}请各位高手赐教!
(2)如果点了重选(主要工作就是撤销Gridview的绑定,以及Checkboxlist选项清空),再次选择时,如果上次已选的项本次未选,,点击查询时,在“ GridView1.DataBind();”处会提示对应的字段找不到。这个原因应该是与1有关系。
重选代码:protected void Button2_Click(object sender, EventArgs e)
{
for (Int32 i = 0; i < CheckBoxList1.Items.Count; i++)
{
CheckBoxList1.Items[i].Selected = false;
}
GridView1.DataSource = null;
GridView1.DataBind();
GridView1.Visible = false; }查询代码:
protected void Button1_Click(object sender, EventArgs e)
{
string ConStr = "server = .\\sqlexpress;user id=sa;pwd=sa;database = gridview";
SqlConnection con = new SqlConnection(ConStr); String sqlStr = "";
for (Int32 i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
sqlStr += CheckBoxList1.Items[i].Value + ",";
}
}
sqlStr = sqlStr.Trim(','); String sql = "Select " + sqlStr + " from userinfo";
SqlDataAdapter ada = new SqlDataAdapter(sql, con);
con.Open(); DataSet ds = new DataSet();
ada.Fill(ds, "tt");
GridView1.Visible = true;
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.Columns.Clear(); for (Int32 i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{ BoundField col = new BoundField();
col.HeaderText = CheckBoxList1.Items[i].Text;
col.DataField = CheckBoxList1.Items[i].Value;
col.Visible = true;
GridView1.Columns.Add(col);
}
} con.Close();
}请各位高手赐教!
解决方案 »
- 搜索关键字的颜色问题
- 在业务层中需要验证参数吗??
- gridview单击事件的如何响应
- 求解密 eval function ///6
- 我想在前一页 下一页中间显示123456789这样的页码,当是第一页的时候前一页不显示,有办法吗??
- 自已编写的类库调用时出现“已在多处定义"的警告怎么解决?
- 高手救命:Table 导出excel时,如何控制 打印选项
- 怎样和xslt调用函数生成的xml格式的字符串
- 用过Jmail的兄弟请进!这个问题有难度!!!!(高分)
- asp.net站点"您所请求的URL无法获取"
- .NET Framework Data Provider
- 菜鸟问题:网上源码中文件夹名称的含义是什么?
再
GridView1.DataSource = ds;
GridView1.DataBind();
遍历CheckBoxList1.Items.Count
this.GridView1.Columns[CheckBoxList1.Items[i].Value].Visible =CheckBoxList1.Items[i].Checked