我需要一个GridCheckBoxColumn包含来自我的表SQL的字符串值(id)。 OBS:我使用ASP.NET框架4,Telerik,C#和SQL Server。
情景:
我的代码asp.net
<telerik:GridCheckBoxColumn DataField="securityGroupR" HeaderText="Access to Modify"
SortExpression="securityGroupR" UniqueName="securityGroupR" DataType="System.String">
</telerik:GridCheckBoxColumn>
<telerik:GridCheckBoxColumn DataField="securityGroupM" HeaderText="Access to Modify"
SortExpression="securityGroupM" UniqueName="securityGroupM" DataType="System.String">
</telerik:GridCheckBoxColumn>
错误:字符串未被识别为有效的布尔值。
如何为每个选定的行创建传递我的.CS组名的GridCheckBox?
韩国社交协会!
答案 0 :(得分:1)
请尝试使用以下代码段。
.ASPX
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
OnItemDataBound="RadGrid1_ItemDataBound">
<MasterTableView>
<Columns>
<telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get selected Checbox" />
.ASPX.CS
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("flag", typeof(string));
dt.Rows.Add(1, "true");
dt.Rows.Add(2, "true");
dt.Rows.Add(3, "false");
RadGrid1.DataSource = dt;
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
DataRowView dr = item.DataItem as DataRowView; // Convert DataItem into Your Assigned Object
(item.FindControl("CheckBox1") as CheckBox).Checked = GetBoolValueFromString(Convert.ToString(dr["flag"]));
}
}
protected bool GetBoolValueFromString(string strFlag)
{
bool flag = false;
bool.TryParse(strFlag, out flag);
return flag;
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
if ((item.FindControl("CheckBox1") as CheckBox).Checked)
{
string strID = item["ID"].Text; // Get selected Checkbox's ID Field Value
}
}
}