从 dataGridViewComboBoxColumn 中删除重复值

发布于 2024-10-30 22:51:35 字数 1028 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么敢 2024-11-06 22:51:35

更改 SELECT 语句以返回 remedy 的不同值:

string sql = "SELECT DISTINCT remedy FROM Table1;"

以及如果您希望对其进行排序:

string sql = "SELECT DISTINCT remedy FROM Table1 ORDER BY remedy ASC;"

string sql = "SELECT DISTINCT remedy FROM Table1 ORDER BY remedy DESC;"

change the SELECT statement to return distinct values of remedy:

string sql = "SELECT DISTINCT remedy FROM Table1;"

and if you want it ordered:

string sql = "SELECT DISTINCT remedy FROM Table1 ORDER BY remedy ASC;"

or

string sql = "SELECT DISTINCT remedy FROM Table1 ORDER BY remedy DESC;"
风筝在阴天搁浅。 2024-11-06 22:51:35

除了将 unique 语句应用于您的 SQL 之外,在创建第二个表时,还可以对原始 DataTable 应用 unique 语句,如下所示(包含我用于原型设计的单元测试和辅助类)这)。

我在下面添加了一条注释来突出显示应用不同行的行 - 您使用的是 .ToTable() 方法,该方法采用布尔参数 Distinct 来指定仅返回不同行。

[TestMethod]
public void CreateDistinctDataTable()
{
    DataTable originalTable = CreateDataTable();

    AddDataToTable("Fred", "Bloggs", originalTable);
    AddDataToTable("Fred", "Bloggs", originalTable);
    AddDataToTable("John", "Doe", originalTable);

    // This is the key line of code where we use the .ToTable() method
    DataTable distinctTable = originalTable.DefaultView.ToTable( /*distinct*/ true);

    // The original table has two rows with firstname of Fred
    Assert.AreEqual(2, originalTable.Select("firstname = 'Fred'").Length);

    // The new table only has one row with firstname of Fred
    Assert.AreEqual(1, distinctTable.Select("firstname = 'Fred'").Length);
}

private DataTable CreateDataTable()
{
    DataTable myDataTable = new DataTable();

    DataColumn myDataColumn;

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "firstname";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "lastname";
    myDataTable.Columns.Add(myDataColumn);

    return myDataTable;
}

private void AddDataToTable(string firstname,  string lastname, DataTable myTable)
{
    DataRow row = myTable.NewRow();

    row["firstname"] = firstname;            
    row["lastname"] = lastname;

    myTable.Rows.Add(row);
} 

另一种想法是我建议不要在 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.

[TestMethod]
public void CreateDistinctDataTable()
{
    DataTable originalTable = CreateDataTable();

    AddDataToTable("Fred", "Bloggs", originalTable);
    AddDataToTable("Fred", "Bloggs", originalTable);
    AddDataToTable("John", "Doe", originalTable);

    // This is the key line of code where we use the .ToTable() method
    DataTable distinctTable = originalTable.DefaultView.ToTable( /*distinct*/ true);

    // The original table has two rows with firstname of Fred
    Assert.AreEqual(2, originalTable.Select("firstname = 'Fred'").Length);

    // The new table only has one row with firstname of Fred
    Assert.AreEqual(1, distinctTable.Select("firstname = 'Fred'").Length);
}

private DataTable CreateDataTable()
{
    DataTable myDataTable = new DataTable();

    DataColumn myDataColumn;

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "firstname";
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn();
    myDataColumn.DataType = Type.GetType("System.String");
    myDataColumn.ColumnName = "lastname";
    myDataTable.Columns.Add(myDataColumn);

    return myDataTable;
}

private void AddDataToTable(string firstname,  string lastname, DataTable myTable)
{
    DataRow row = myTable.NewRow();

    row["firstname"] = firstname;            
    row["lastname"] = lastname;

    myTable.Rows.Add(row);
} 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文