从 dataGridViewComboBoxColumn 中删除重复值
我使用 DataGridViewComboBoxColumn 在 DataGridView 中创建 ComboBox,但我的 ComboBox 不够好。我需要我的 ComboBox
上不包含重复的值。这是一个例子:
苹果
黑莓
Chrome
苹果
我想删除出现多次的值。我怎样才能做到这一点?
这是我的代码:
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
Dataset data = new Dataset();
OleDbDataAdapter adapter = new OleDbDataAdapter();
string path = "Data Source = "+".\\"+"test.accdb";
string conStr = "Provider = Microsoft.ACE.OleDb.12.0;"+@path;
conn.Open();
string sql = "SELECT * FROM Table1;"
cmd = new OleDbCommand(sql,conn);
adapter = new OleDbDataAdapter(cmd);
data = new Dataset();
adapter.Fill(data,"Table1");
DataGridViewComboBoxColumn testcolumn = new DataGridViewComboBoxColumn();
testcolumn.Name = "test na ja";
testcolumn.Datasource = data.table[0];
testcolumn.ValueMember = "Remedy";
testcolumn.DisplayMember = "Remedy";
dataGridview1.Columns.Add(testcolumn);
conn.Close()
I use DataGridViewComboBoxColumn
to make a ComboBox
in DataGridView
but my ComboBox
isn't good enough. I need my ComboBox
to not have repeated values on it. This is an example:
Apple
Blackberry
Chrome
Apple
I want to remove the values that appear more than one time. How can I do that?
This is my code:
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
Dataset data = new Dataset();
OleDbDataAdapter adapter = new OleDbDataAdapter();
string path = "Data Source = "+".\\"+"test.accdb";
string conStr = "Provider = Microsoft.ACE.OleDb.12.0;"+@path;
conn.Open();
string sql = "SELECT * FROM Table1;"
cmd = new OleDbCommand(sql,conn);
adapter = new OleDbDataAdapter(cmd);
data = new Dataset();
adapter.Fill(data,"Table1");
DataGridViewComboBoxColumn testcolumn = new DataGridViewComboBoxColumn();
testcolumn.Name = "test na ja";
testcolumn.Datasource = data.table[0];
testcolumn.ValueMember = "Remedy";
testcolumn.DisplayMember = "Remedy";
dataGridview1.Columns.Add(testcolumn);
conn.Close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
SELECT
语句以返回remedy
的不同值:以及如果您希望对其进行排序:
或
change the
SELECT
statement to return distinct values ofremedy
:and if you want it ordered:
or
除了将 unique 语句应用于您的 SQL 之外,在创建第二个表时,还可以对原始
DataTable
应用 unique 语句,如下所示(包含我用于原型设计的单元测试和辅助类)这)。我在下面添加了一条注释来突出显示应用不同行的行 - 您使用的是 .ToTable() 方法,该方法采用布尔参数 Distinct 来指定仅返回不同行。
另一种想法是我建议不要在 SQL 语句中从表中选择 *。如果添加更多列(尤其是 blob 之类的列),这可能会对性能产生影响,并且还可能意味着您获得的列会破坏查询的独特性质。
As well as applying the distinct statement to your SQL it is possible to apply a distinct to the original
DataTable
when creating a second table as shown below (with a unit test and helper classes included that I used to prototype this).I've added a comment below to highlight the line where the distinct is applied - what you use is the .ToTable() method which takes the Boolean parameter Distinct to specify only return distinct rows.
One additional thought is I would suggest not selecting * from your table in the SQL statement. This can have performance implications down the track if more columns are added (particularly things like blobs) and could also mean you get columns that break the distinct nature of your query.