从SQL逗号分隔的字符串

时间:2017-02-25 10:52:49

标签: c# asp.net gridview

这是我的情况。

我已经从CheckBoxList保存了数据:

foreach (ListItem li in CheckBoxList1.Items)
{
    Steps = String.Join(", ", CheckBoxList1.Items.Cast<ListItem>()
                                     .Where(i => i.Selected));
}

它运行正常,但我想要的是从DataGrid中的checkBoxList中的数据库中获取这些数据。

以下是我尝试过的很多事情。请帮忙:

&#13;
&#13;
 <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>
&#13;
&#13;
&#13;

背后的C#代码

  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();
        }
    }

1 个答案:

答案 0 :(得分:0)

嘿谢谢我通过为Steps创建另一个表来解决它,并且对于Documents Required ,,它现在显示我想要进入datagrid的checkBoxList并且显示代码

 if ((e.Row.RowState & DataControlRowState.Edit) == 0)
        {
            CheckBoxList c = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
            CheckBoxList CheckBoxListDR = (CheckBoxList)e.Row.FindControl("CheckBoxList3");
            DataTable dt;
            String SQL = "SELECT distinct S.Steps, D.DocumentsRequired, T.TransactionID FROM [StepsIsSelectedTable] S JOIN TransactionDetail T " +
                " ON S.TransactionIDSteps = T.TransactionID JOIN DocumentsRequiredIsSelected D " +
                "ON D.TransactionIDDR = T.TransactionID  WHERE " +
                "TransactionIDDR ='" + TreeView1.SelectedValue + "' and S.StepsIsSelected='True' and D.DocumentsRequiredIsSelected='True'" +
                " group by S.Steps, D.DocumentsRequired ,T.TransactionID";

            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);
                    }
                }

                c.DataSource = dt;
                CheckBoxListDR.DataSource = dt;
                c.DataTextField = "Steps";
                c.DataValueField = "TransactionID";
                CheckBoxListDR.DataTextField = "DocumentsRequired";
                CheckBoxListDR.DataValueField = "TransactionID";
                c.DataBind();
                c.SelectedValue = "TransactionID";
                CheckBoxListDR.DataBind();
                CheckBoxListDR.SelectedValue = "1";
            }
        }