C#中如何清理comboBox中的值和内容?
comboBox我是绑定数据库的!comboBox.Items.clear()这些都不行,谁教教我?...
comboBox我是绑定数据库的!comboBox.Items.clear() 这些都不行,谁教教我?
展开
4个回答
2013-05-30
展开全部
你这到底是什么意思啊?你绑定数据库了还能清空么,除非你数据库没有值。。显然不可能;你是不是想先清空在往里边放值啊?先用clear(),然后用add()?如果是这样的话你可以在后台手动绑定,别用数据库控件绑定;在后台你就可以先用clear()在用add();
2013-05-30
展开全部
comboBox1.DataSource = null
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-05-30
展开全部
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection conn;
private SqlCommand comm;
private SqlDataAdapter da;
private DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
d();
}
private void d()//绑定数据库的代码
{
conn = new SqlConnection();
conn.ConnectionString = "server=(local);initial catalog=你好;integrated security=true";
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select 班级编号 from 班级信息表";
da = new SqlDataAdapter();
da.SelectCommand = comm;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
dt = new DataTable();
int count=da.Fill(dt);
if (count != 0)
{
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "班级编号";
this.comboBox1.ValueMember = "班级编号";
}
} private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.Items.Count != 0)//c#后台代码
{
this.comboBox1.SelectedValue = "";
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection conn;
private SqlCommand comm;
private SqlDataAdapter da;
private DataTable dt;
private void Form1_Load(object sender, EventArgs e)
{
d();
}
private void d()//绑定数据库的代码
{
conn = new SqlConnection();
conn.ConnectionString = "server=(local);initial catalog=你好;integrated security=true";
comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select 班级编号 from 班级信息表";
da = new SqlDataAdapter();
da.SelectCommand = comm;
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
dt = new DataTable();
int count=da.Fill(dt);
if (count != 0)
{
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "班级编号";
this.comboBox1.ValueMember = "班级编号";
}
} private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.Items.Count != 0)//c#后台代码
{
this.comboBox1.SelectedValue = "";
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-05-30
展开全部
comboBox.text="";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询