0
這是我的情況。從SQL逗號分隔的字符串重新填充GridView中的複選框列表
我從的CheckBoxList保存的數據:
foreach (ListItem li in CheckBoxList1.Items)
{
Steps = String.Join(", ", CheckBoxList1.Items.Cast<ListItem>()
.Where(i => i.Selected));
}
它工作正常,但我要的是要回從這個數據庫中的數據一個CheckBoxList的DataGrid中。
波紋管是我嘗試過的很多東西。請幫助:
<div align="center" style="margin-top: 50px;">
<asp:GridView ID="GridView1" runat="server" BackColor="White" DataKeyNames="SNo"
OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Vertical" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="TransactionName" HeaderText="Transaction Name" />
<asp:BoundField DataField="Category" HeaderText="Category" />
<asp:TemplateField HeaderText="Steps">
<ItemTemplate>
<asp:Label ID="lblBrand" runat="server" Text='<%#Eval("Steps") %>'>
</asp:Label>
<asp:CheckBoxList ID="CheckBoxList2" runat="server">
</asp:CheckBoxList>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<EditRowStyle Height="10px" Width="2px" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
背後
private void Bind_CheckBoxList()
{
DataTable dt;
String SQL = "SELECT * from TransactionDetail";
string sConstr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(sConstr))
{
using (SqlCommand comm = new SqlCommand(SQL, conn))
{
conn.Open();
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
dt = new DataTable("tbl");
da.Fill(dt);
}
conn.Close();
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBoxList c = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
DataTable dt;
String SQL = "SELECT * FROM TransactionDetail";
string sConstr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(sConstr))
{
using (SqlCommand comm = new SqlCommand(SQL, conn))
{
conn.Open();
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
if (c != null)
{
Label test = (Label)(e.Row.FindControl("lblBrand"));
Steps = test.Text;
string[] items = Steps.Split(new[] { ", " }, StringSplitOptions.None);
foreach (ListItem li in c.Items)
li.Selected = items.Contains(li.Text);
}
dt = new DataTable("tbl");
da.Fill(dt);
}
}
c.DataSource = dt;
c.DataTextField = what to write here to pass the array Text;
c.DataValueField = "SNo";
c.DataBind();
}
}
您基本上會做相反的事情,將字符串拆分,循環數組中的所有項並檢查它們是否存在於OnRowDataBound事件中的CheckBoxList中。 – VDWWD
請檢查更新......它仍然不起作用 –
您的代碼本身是正確的。測試它,它的工作原理。檢查分割後的'items'數組中的項目,看它們是否與'c'中的項目匹配 – VDWWD