WinForm中CheckListBox控件的应用
来源:互联网 发布:删除列sql语句 编辑:程序博客网 时间:2024/05/19 06:49
CheckedListBox如何绑定数据库显示数据项?
//加载字段到checkboxlist
IList<string> list1 = dataop.GetTableColumnsName(e.Node.Parent.Name);//调用
for (int j = 0; j < list1.Count; j++)
{
chklColumn.Items.Add(list1[j], true); //添加显示字段,作为多选项
//其中true表示默认均选择
}
********************************
#region 获取表的字段名称 //链接数据库的内容代码省略
public IList<string> GetTableColumnsName(string tablename)
{
SqlConnection conn;
DataTable dt;
GetTableComm(tablename, out conn, out dt);
IList<string> list1 = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
list1.Add(dt.Rows[i]["字段名"].ToString());
}
conn.Close();
return list1;
}
#endregion
**********************************
【【【【【【【【【【【华丽丽的分割线【【【【【【【【【【【【【【【
《1》this.checklistboxUser.DataSource = ds.Tables["table1"]; //adp.Fill(ds,"table1")
//看情况,或者ds.Tables[0]; 其他是那样的adp.Fill(ds),低级的就不说了
this.checklistboxUser.DisplayMember = "UserName";
this.checklistboxUser.ValueMember = "UserID";
《2》如何获取checkedlistBox的DisplayMember和ValueMember
for(int i=0; i< checklbUser.Items.Count; i++)
{
if(checklbUser.GetItemChecked(i)==true)
{
MessageBox.Show(checklbUser.GetItemText(checklbUser.Items[i]));//获取DisplayMember
MessageBox.Show(ds.Tables["table1"].Rows[i]["UserID"].ToString() );//获取ValueMember
}
}
《3》获取所有选中项
if (this.checkedListBox1.CheckedItems.Count > 0)
{
foreach (string item in this.checkedListBox1.CheckedItems)
{
//item就是你要插入数据库的数据
}
《4》一些可以借鉴的例子
//从另一张数据库表里面获得一个用户的所有角色并让checkListBox里面的对应项选中。【显示】
public
void
checkBind()
{
ds = dataoperate.getDs(
"select roletype from role_table where userid='"
+ userid +
"'"
,
"role_table"
);
foreach
(DataRow dr
in
ds.Tables[0].Rows)
{
int
i =
this
.checkedListBox1.FindString(dr[
"roletype"
].ToString());
if
(i>=0)
{
this
.checkedListBox1.SetItemChecked(i,
true
);
}
}
}
//根据的CheckListBox里面的选中项来更新用户的角色。【更改】
public
void
updatRole()
{
DeleteRoleById(userid);
//先删除用户的所有角色,以免重复
string
struserid;
string
strusertype;
for
(
int
i=0;i<
this
.checkedListBox1.CheckedItems.Count;i++)
{
string
roletype
=
this
.checkedListBox1.CheckedItems[i].ToString();
//插入操作
insert into rol_table (userid,roletype) values(
'" + struserid + "'
,
'" + strusertype + "'
)");
}
}
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
private string GetChklColumn()
{
string strColumn = "";
for (int i = 0; i < chklColumn.CheckedItems.Count; i++)
{
strColumn += "'"+chklColumn.CheckedItems[i].ToString() + "',";
}
if (strColumn.Length > 0)
{
strColumn = strColumn.Remove(strColumn.Length - 1, 1);
}
else
{
strColumn = "''";
}
return strColumn;
}
#endregion
- WinForm中CheckListBox控件的应用
- C#Winform中CheckBox、ChecklistBox控件遍历代码 (asp.net)
- CheckListBox控件的使用
- winform c# 中获取CheckListBox 值
- C#控件CheckListBox的使用
- WinForm中关于DataGridView控件的一些应用
- 制作一个CheckListBox控件
- checklistbox控件用法总结
- winform中常用的控件
- winform中常用的控件
- Winform中扫描仪的应用
- C#控件学习一:CheckListBox
- 在windowform 中checklistbox的数据绑定 怎么做
- CheckListBox的一些用法!
- CheckListBox的使用(代码)
- c# checklistbox的用法
- c# checklistbox的用法
- c# checklistbox的用法
- int 不能转换为short
- Windows下使用Python读取Excel表格数据
- NYOJ-845-无主之地1-2013年11月3日00:08:18
- 进程互斥----ttylock()类似原理实现
- ZZ: linux IPV4 IPV6 sockaddr 几个结构体及其关系: sockaddr sockaddr_in sockaddr_in6
- WinForm中CheckListBox控件的应用
- Ubuntu 升级、卸载内核、安装VmTools
- NSString的常用方法
- 关于共享内存的一个题目(未完)
- Android 下用 Pull 解析和生成 XML
- 山东理工大学ACM平台题答案 1121 C语言实验——for循环打印图形(循环结构
- 第三周 看struts2的一周 混日子的一周- -
- 学习用网站
- NYOJ-95-众数问题-2013年11月3日12:03:08