我有一个嵌套网格我想在另一个表格中存在datakey时检查网格中的列
网格就像这样
<asp:GridView Width="100%" DataKeyNames="PK_ServiceTypeID" HorizontalAlign="Center" runat="server" OnRowDataBound="gridServiceLocations_RowDataBound" AutoGenerateColumns="false" ID="gridServiceLocations" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor: pointer" src="../../Images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" Width="100%" DataKeyNames="PK_ServiceDetailID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Service" HeaderText="Service" />
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ServiceTypeName" HeaderText="ServiceTypeName" HeaderStyle-HorizontalAlign="Center" />
</Columns>
</asp:GridView>
我已经从后面的代码绑定了这个网格
答案 0 :(得分:0)
如果我理解,我认为类似的东西会起作用:
首先,您需要使用明确的数据:从您的sql列获取值并将其存储到字符串(或任何您想要的类型)数组或List中,如下所示:
//Get your column value from your db
string myValueFromDb = "0000000062,0000000034,0000000016,0000000174,0000000055";
//Split the values into an list of string
myValues = new List<string>(myValueFromDb.Split(','));
//Here you have all your db values stored in the list myValues
但要访问您的复选框,您必须添加ID。
喜欢:asp:CheckBox runat="server" ID="checkBox" />
现在,您需要检查gridview的每一行。这样的事情:
protected void gridServiceLocations_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != -1)
{
//Get the value of the current row DataKey
string dataKey = grid_view.DataKeys[e.Row.RowIndex].Value.ToString();
//GET THE ROW
GridViewRow row = e.Row;
//Now compare it with your value
if (myValues.Contains(dataKey))
{
//CHECK CASE
//The index of column of the column where the checkbox are (1 here, change it )
CheckBox checkbox = ((CheckBox)row.Cells[1].FindControl("checkBox"));
checkbox.Checked = true;
}
else
{
//UNCHECK CASE
CheckBox checkbox = ((CheckBox)row.Cells[1].FindControl("checkBox"));
checkbox.Checked = false;
}
}
}