I am moving my first steps with databases and databinding, and I'm trying to figure some things out.
In my Access db, I have a table (Items) with string fields such as Description, Code and Comments. I also have additional boolean fields such as Availability and Relevance.
"Items"
| ID | Description | Code | Comments | Availability | Relevance |
| | | | | | |
| 1 | Apple | AP | Red | x | |
| 2 | Orange | OR | Orange | x | x |
| 3 | Banana | BN | Yellow | | x |
| 4 | Lime | LM | Green | x | |
I want to display some of this data in a dataGridView via databinding: mostly Description, Code and Comments.
private void DataBindGridView()
{
string dbPath = @"C:\FruitDB.accdb";
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbPath + ";Persist Security Info=False;";
OleDbConnection dbConn = new OleDbConnection(connStr);
dbConn.Open();
string query = "SELECT Description, Code, Comments, FROM Items ORDER BY ID ASC";
OleDbCommand getItems = new OleDbCommand(query);
OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter(getItems);
DataTable itemsTable = new DataTable();
itemsTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
dbDataAdapter.SelectCommand.Connection = dbConn;
dbDataAdapter.Fill(itemsTable);
// I want my first column to contain a checkbox for other purposes
//
DataGridViewCheckBoxColumn col1 = new DataGridViewCheckBoxColumn(false);
col1.Name = "Selection";
col1.HeaderText = "";
dataGridView1.Columns.Add(col1);
// Binding the dataGridView
//
dataGridView1.ReadOnly = false;
dataGridView1.DataSource = itemsTable;
// The user is allowed to edit the checkbox column only
//
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
if (col.Index == 0)
{
col.ReadOnly = false;
}
else
{
col.ReadOnly = true;
}
}
}
Now, even though I don't want to display the Availability or Relevance information as separate columns in the dataGridView, I do want that information to be shown in some other way, e.g. as a strikethrough font or by setting the line color to grey.
How do I databind these properties? Do I need to repeat the whole thing with a different another query / command / dataTable...?
Aucun commentaire:
Enregistrer un commentaire