使用数据库中的项填充checkboxlist?

时间:2014-04-25 19:54:08

标签: c# checkboxlist mdf sqlclient

好的,所以我想将一些数据填充/绑定到一个复选框列表,但似乎无法将正确的值binf?我想用ROW中的信息填充它,而不是整个列,这就是我发生的事情。无论如何,这里有一些代码可以向您展示问题所在。

这是xaml中的代码

<form id="form1" runat="server">
<div>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Option1], [Option2], [Option3] FROM [Questions] WHERE ([QuestionID] = @QuestionID)">
        <SelectParameters>
            <asp:Parameter DefaultValue="1" Name="QuestionID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

</div>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="QuestionID" DataValueField="QuestionID">
    </asp:CheckBoxList>
</form>

这是我的数据库(只是一个例子) 链接来自gyazo

Questions

Data


这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace ExampleCheckbox
{
public partial class Question_One : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand();
        command.Connection = con;
        command.CommandType = CommandType.Text;
        command.CommandText = "Select * from Questions";

        SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
        DataSet questionsDataSet = new DataSet();


            con.Open();
            dataAdapter.Fill(questionsDataSet, "Question");

            DataTable dt = questionsDataSet.Tables["Question"];

            foreach (DataRow dr in dt.Rows)
            {
                ListItem newItem = new ListItem(dr["Option1"].ToString(), dr["QuestionID"].ToString());
                CheckBoxList1.Items.Add(newItem);
            }

            CheckBoxList1.DataSource = questionsDataSet;
            CheckBoxList1.DataTextField = "Option1";
            CheckBoxList1.DataValueField = "QuestionID";
            CheckBoxList1.DataBind();
    }
}

}


这也是我遇到的问题 PROBLEM

由于

1 个答案:

答案 0 :(得分:3)

您要做的是循环遍历列而不是行,并取代列名。

在这里查看这个问题/答案:

http://social.msdn.microsoft.com/Forums/en-US/4b6ede3b-093d-46f1-8766-d4a96608997d/loop-thru-a-datatable-for-columnnames-columnvalues?forum=csharpgeneral

这是一个完整的例子。这就是您的代码应该是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace ExampleCheckbox
{
public partial class Question_One : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand();
        command.Connection = con;
        command.CommandType = CommandType.Text;
        command.CommandText = "Select * from Questions";

        SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
        DataSet questionsDataSet = new DataSet();


            con.Open();
            dataAdapter.Fill(questionsDataSet, "Question");

            DataTable dt = questionsDataSet.Tables["Question"];

            int i = 0;
            string str1 = string.Empty;
        int i = 0;
                    dr = dt.Rows(ClientID);   //whatever you're using for the row index
            foreach (DataColumn dc in dt.Columns)
            {
                ListItem newItem = new ListItem(dr[dc].ToString(), i.ToString());
                CheckBoxList1.Items.Add(newItem);
                i++;
            }
        }
    } 
        } 

}