C#CheckedListBox控件与数据库

智者潜行 2008-12-25 02:36:06
以下是我写的方法:

public void Show(CheckedListBox clb)
{
string sql = "select Cam_Code,Cam_Flag from ST_Cam";
DataSet ds = SqlHelper.ExecuteDataset(db.ConnectionString, CommandType.Text, sql);
DataTable dt = new DataTable();
dt = ds.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
clb.Items.Add(dt.Rows[i][0]);//把每一项添加到CheckedListBox
if (dt.Rows[i][1].ToString() == "1")
{
//如果数据为1了,CheckedListBox 的选项就会选上,勾选上
clb.SetItemChecked(i,true);
}
}
}

请问,我在每一项修改了他的Checked后,就是有的勾选项更改了,怎么存回到数据库呢?
就是说选上的项,数据库会存为1;没有选上的项,数据库会存为0
...全文
532 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangping_li 2008-12-25
  • 打赏
  • 举报
回复
ItemCheck事件是指定某项属性将要更改,直到事件发生后,该值才会更新
用这个事件可以啊
yyq136 2008-12-25
  • 打赏
  • 举报
回复
onTextChange吧
Zhanlixin 2008-12-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lfoy112 的回复:]
这个不错,不过这样会增加了服务器的负担!
请问checkedListBox1的Checked改变时的事件是什么?就是产,我改变一条我就执行一条..但不知事件是什么,ItemCheck事件不行.只有选中才执行的.
引用 3 楼 Zhanlixin 的回复:
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
string strCode=checkedListBox1.GetItemText(this.checkedListBox1.Items[i]);
i…
[/Quote]

我试过了不管选中还是不选中都执行
Zhanlixin 2008-12-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lfoy112 的回复:]
这个不错,不过这样会增加了服务器的负担!
请问checkedListBox1的Checked改变时的事件是什么?就是产,我改变一条我就执行一条..但不知事件是什么,ItemCheck事件不行.只有选中才执行的.
引用 3 楼 Zhanlixin 的回复:
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
string strCode=checkedListBox1.GetItemText(this.checkedListBox1.Items[i]);
i…
[/Quote]

checkedListBox1_ItemCheck
PowerFedora 2008-12-25
  • 打赏
  • 举报
回复
autoback=true
wangping_li 2008-12-25
  • 打赏
  • 举报
回复

void ch_ItemCheck(object sender, ItemCheckEventArgs e)
{

}
智者潜行 2008-12-25
  • 打赏
  • 举报
回复
这个不错,不过这样会增加了服务器的负担!
请问checkedListBox1的Checked改变时的事件是什么?就是产,我改变一条我就执行一条..但不知事件是什么,ItemCheck事件不行.只有选中才执行的.
[Quote=引用 3 楼 Zhanlixin 的回复:]
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
string strCode=checkedListBox1.GetItemText(this.checkedListBox1.Items[i]);
if (this.checkedListBox1.GetItemChecked(i) == true)
{
string strSQL="update ST_Cam set Cam_Flag='1' where Cam_Code='"+strCode +"'";
}

[/Quote]
cpio 2008-12-25
  • 打赏
  • 举报
回复
for (int i = 0; i < clb.Item.Count; i++)
{
string sql = "update ST_Cam Set Cam_Flag=";
sql += clb.GetItemChecked(i) ? "1" : "0";
sql += " Where Cam_Code='" + clb.Items[i].ToString() + "'";
SqlHelper.ExecuteSql(sql);
}
wuyq11 2008-12-25
  • 打赏
  • 举报
回复
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.Items[i].ToString() == "1")
{
checkedListBox1.SetItemChecked(i, true);

}

}
Zhanlixin 2008-12-25
  • 打赏
  • 举报
回复
for (int i = 0; i < this.checkedListBox1.Items.Count; i++)
{
string strCode=checkedListBox1.GetItemText(this.checkedListBox1.Items[i]);
if (this.checkedListBox1.GetItemChecked(i) == true)
{
string strSQL="update ST_Cam set Cam_Flag='1' where Cam_Code='"+strCode +"'";
}
else
{
string strSQL="update ST_Cam set Cam_Flag='1' where Cam_Code='"+strCode +"'";
}
//执行strSQL
}
优途科技 2008-12-25
  • 打赏
  • 举报
回复

给你一个精典操作数据库类。


class DBcon
{
private string strSQL;
//与SQL Server的连接字符串设置
private string connectionString;
//与数据库的连接
private SqlConnection myConnection;

private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da;


/////////////////////////////////操作脱机数据库(创建了该类的实例时直接用) /////////////////////////////////////////////////////

//根据输入的SQL语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL,string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.ds.Clear();
this.da.Fill(ds,tempTableName);
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
}

//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet,string tableName)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet,tableName);
return changedDataSet;//返回更新了的数据库表
}

/////////////////////////////////直接操作数据库(未创建该类的实例时直接用) /////////////////////////////////////////////////////

//检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL,this.myConnection);
this.da.Fill(tempDataSet);
return tempDataSet.Tables[0];
}

//数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL,this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
return intNumber;
}

caofan520 2008-12-25
  • 打赏
  • 举报
回复
clb.Items[i].Value = dt.Rows[i][1].ToString();
取的时间取他的Value就可以了

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧