如何使用DB中的类别和子类别填充复选框列表 [英] How to fill checkbox list with category and sub-category from DB
本文介绍了如何使用DB中的类别和子类别填充复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
如何用DB
填写带有类别和子类别的复选框列表这是我的sql表
< ; pre lang =SQL> SELECT
dbo.category_data.category_id,
dbo.category_data.category_name,
dbo.category_data.category_parent,
dbo.category_data.category_description,
dbo.category_data.category_image,
dbo.category_data.category_status
FROM [dbo]。[category_data]
这是我的c#代码我需要帮助来完成它
private void bind_cat_menu()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings [ QMALL]。ToString();
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = Select * from( category_data];
SqlDataAdapter da = new SqlDataAdapter(sql,con);
da.Fill(ds);
dt = ds.Tables [ 0 ];
DataRow [] drowparent = dt.Select( category_parent = + 0 );
foreach (DataRow dr in drowparent)
{
}
foreach (DataRow dr in dt.Select( category_parent> + 0 ))
{
}
我的尝试:
private void bind_cat_menu()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings [QMALL]。ToString();
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql =Select * from(category_data];
SqlDataAdapter da = new SqlDataAdapter(sql,con);
da.Fill(ds);
dt = ds。 Tables [0];
DataRow [] drowparent = dt.Select(category_parent =+ 0);
foreach(DataRow dr in drowparent)
{
}
foreach(DataRow dr in dt.Select(category_parent>+ 0))
{
}
解决方案
这是一个获取类别名称和状态并填充复选框的示例 - 您需要根据自己的需要调整它。
我假设category_status
等于true
或false
- 例如它可能是一个位
列,其中0 = false,1 = true。
我没有对类别感到不满和子类别,因为你没有真正描述这种关系,因为该示例列出了表中的 all ,无论他们是否有父母。
最后要注意的一点是 - 它使用从(category_data)$选择 * 是不好的做法p $ p>更好的是使用您需要的显式列名:SELECT category_name,category_status FROM category_data这是一个填充ComboBoxList并根据category_status自动设置所选值的工作示例。
< span class =code-keyword> privatevoid BindCheckBoxList()
{
var constr = ConfigurationManager.ConnectionStrings [ ConnectToDB]。ConnectionString ;
使用( var con = new SqlConnection(constr))
{
使用( var cmd = < span class =code-keyword> new SqlCommand( SELECT category_name,category_status FROM category_data))
{
使用( var sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
使用( var ds = new DataSet())
{
sda.Fill(ds);
if (ds.Tables [ 0 ]。Rows.Count < = 0 ) return ; // 如果表格中没有数据,则无事可做
CheckBoxList1.DataSource = ds;
CheckBoxList1.DataTextField = category_name;
CheckBoxList1.DataValueField = category_name;
CheckBoxList1.DataBind();
var i = 0 ;
foreach (DataRow r in ds.Tables [ 0 ]。行)
{
Debug.Print( {0 } {1},r.ItemArray [ 0 ],r.ItemArray [ 1 ]);
if (r.ItemArray [ 1 ]。ToString()。ToLower()== true)
CheckBoxList1.Items [i] .Selected = 真跨度>;
i ++;
}
}
}
}
}
hello, how to fill checkbox list with category and sub-category from DB this is my sql table <pre lang="SQL">SELECT dbo.category_data.category_id, dbo.category_data.category_name, dbo.category_data.category_parent, dbo.category_data.category_description, dbo.category_data.category_image, dbo.category_data.category_status FROM [dbo].[category_data]
and this is my c# code i need help to complete it
private void bind_cat_menu()
{
SqlConnection con= new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["QMALL"].ToString();
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
string sql = "Select * from (category_data]";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
dt = ds.Tables[0];
DataRow[] drowparent = dt.Select("category_parent=" + 0);
foreach (DataRow dr in drowparent)
{
}
foreach (DataRow dr in dt.Select("category_parent>" + 0))
{
}
What I have tried:
private void bind_cat_menu() { SqlConnection con= new SqlConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings["QMALL"].ToString(); con.Open(); DataSet ds = new DataSet(); DataTable dt = new DataTable(); string sql = "Select * from (category_data]"; SqlDataAdapter da = new SqlDataAdapter(sql, con); da.Fill(ds); dt = ds.Tables[0]; DataRow[] drowparent = dt.Select("category_parent=" + 0); foreach (DataRow dr in drowparent) { } foreach (DataRow dr in dt.Select("category_parent>" + 0)) { }
解决方案
Here is an example that gets the category names and status and populates the checkbox - you will need to adapt it to your own needs.
I've assumed thatcategory_status
equates totrue
orfalse
- for example it might be abit
column where 0 = false and 1 = true.
I haven't faffed around with category and sub-category because you haven't really described the relationship and because the example lists all from the table, regardless of whether they have a "parent" or not.
One final thing to note - it is bad practice to useSelect * from (category_data]Better is use the explicit column names that you need:
SELECT category_name, category_status FROM category_dataHere is a working example that populates a ComboBoxList and automatically sets the selected value based on category_status.
private void BindCheckBoxList() { var constr = ConfigurationManager.ConnectionStrings["ConnectToDB"].ConnectionString; using (var con = new SqlConnection(constr)) { using (var cmd = new SqlCommand("SELECT category_name, category_status FROM category_data")) { using (var sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (var ds = new DataSet()) { sda.Fill(ds); if (ds.Tables[0].Rows.Count <= 0) return; //nothing to do if no data on the table CheckBoxList1.DataSource = ds; CheckBoxList1.DataTextField = "category_name"; CheckBoxList1.DataValueField = "category_name"; CheckBoxList1.DataBind(); var i = 0; foreach (DataRow r in ds.Tables[0].Rows) { Debug.Print("{0} {1}", r.ItemArray[0],r.ItemArray[1]); if (r.ItemArray[1].ToString().ToLower() == "true") CheckBoxList1.Items[i].Selected = true; i++; } } } } }
这篇关于如何使用DB中的类别和子类别填充复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文