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()
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;"
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.
精彩评论