我的DATAGRID绑定一张数据表。
DATAGRID中插入一个CHECKBOX的模版列,怎么遍历来确定哪几行被选中,
同时每次要将选中行的某些字段进行赋值操作

解决方案 »

  1.   

    是指从表中选择么?
    我想到的是 是不是可以用Reader来读
    符合的就选中
    同时赋值我是刚学 呵呵想不到更好的办法了
      

  2.   

    http://www.codeproject.com/aspnet/datagridcheckbox.asp
      

  3.   

    后台2段代码:
    private void DataGrid1_PreRender(object sender, System.EventArgs e)
    {
    foreach (DataGridItem item in this.DataGrid1.Controls[0].Controls)
    {
    if (item.ItemType == ListItemType.Header)
    {
    CheckBox chkAll=(CheckBox)item.FindControl("chkAll");
    System.Text.StringBuilder strScript = new System.Text.StringBuilder("<script language='javascript'> \n");
    strScript.Append("    function checkStatus() { \n");
    strScript.Append("        var bAll = true; \n");
    strScript.Append("        bAll = document.all('" + chkAll.ClientID + "').checked; \n"); for(int i=0; i<DataGrid1.Items.Count ; i++)
    {
    strScript.Append("        document.all('" + DataGrid1.Items[i].Cells[0].FindControl("chkItem").ClientID + "').checked = bAll; \n");
    }
    strScript.Append("    } \n");
    strScript.Append("</script> \n"); if(!Page.IsClientScriptBlockRegistered("checkStatus"))
    {
    Page.RegisterClientScriptBlock("checkStatus",strScript.ToString());
    }
    chkAll.Attributes.Add("onclick","checkStatus()");
    return;
    }
    }
    }private void btnSure_Click(object sender, System.EventArgs e)
    {
    string whereHou ="";
    for (int i = 0 ;i < DataGrid1.Items.Count; i++)
    {
    CheckBox a;
    a = (CheckBox)DataGrid1.Items[i].Cells[0].FindControl("chkItem");
    bool b;
    b = a.Checked;
    if (b == true)
    {
    whereHou +=",'"+DataGrid1.Items[i].Cells[2].Text.ToString()+"'";
    }
    }
    }
    前台:
    <asp:datagrid id="DataGrid1" runat="server" Width="615px" PageSize="8" PagerStyle-Mode="NumericPages"
    PagerStyle-HorizontalAlign="Right" BorderColor="DimGray" BorderWidth="1" GridLines="Both"
    CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd"
    AlternatingItemStyle-BackColor="#eeeeee" HorizontalAlign="Center">
    <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
    <HeaderStyle HorizontalAlign="Center" ForeColor="#404040" BackColor="LightGray"></HeaderStyle>
    <Columns>
    <asp:TemplateColumn>
    <HeaderTemplate>
    <asp:CheckBox id="chkAll" runat="server"></asp:CheckBox>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox id="chkItem" runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
      

  4.   

    For Each theDataGridItem In dGridJob.Items
        '找在DATAGRID里的那个复选择框
                chkFinder = theDataGridItem.FindControl("chkSelectedItem")
                If chkFinder.Checked = True Then
    '选中的就做相应的操作
                End If
            Next
      

  5.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=F43AF9A5-2C2E-4AA6-E976-21E9569F5A8A
      

  6.   

    林子里有好多这样的代码!楼主用 DATAGRID 或 CHECKBOX 搜索就能找到!
      

  7.   

    for(int i=0;i<DataGrid_BudgetItem.Items.Count;i++)
    {
    CheckBox CBox=(CheckBox)this.DataGrid_BudgetItem.Items[i].FindControl("CheckBox1");
    {
    if(CBox.Checked==true)
    {
    h.Add("@CollectItemID",ViewState["CollectItemID"]);
    h.Add("@BudgetItemID",i.ToString());
    obj.InsertRow("Insert_tbgCollectItemBudget",h);
    h.Clear();
    }
    }