将控件属性(例如颜色)数据绑定到未显示的字段
Databinding control properties (e.g. color) to a field which is not displayed
我正在迈出数据库和数据绑定的第一步,我正在尝试解决一些问题。
在我的 Access 数据库中,我有一个 table (Items
),其中包含 Description
、Code
和 Comments
等字符串字段。我还有其他布尔字段,例如 Availability
和 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 | |
我想通过数据绑定在 dataGridView
中显示其中一些数据:主要是 Description
、Code
和 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;
}
}
}
现在,即使我不想在 dataGridView
中将 Availability
或 Relevance
信息显示为单独的列,我确实希望该信息显示在某些其他方式,例如作为删除线字体或将线条颜色设置为灰色。
如何对这些属性进行数据绑定?我是否需要用另一个不同的查询/命令/数据表重复整个过程...?
我想你想要这样的东西:
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView.Columns[e.ColumnIndex].Name == "Availability")
e.CellStyle.ForeColor = Color.Silver;
}
这基本上是将 Availability
列中所有单元格的文本涂成灰色。您可以扩展代码,只是为了演示这个想法。
此外:当然,您绝对应该在 SQL 中包含额外的列。使用 DataGridView
来控制它们的外观。您也可以完全隐藏这些列。
我正在迈出数据库和数据绑定的第一步,我正在尝试解决一些问题。
在我的 Access 数据库中,我有一个 table (Items
),其中包含 Description
、Code
和 Comments
等字符串字段。我还有其他布尔字段,例如 Availability
和 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 | |
我想通过数据绑定在 dataGridView
中显示其中一些数据:主要是 Description
、Code
和 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;
}
}
}
现在,即使我不想在 dataGridView
中将 Availability
或 Relevance
信息显示为单独的列,我确实希望该信息显示在某些其他方式,例如作为删除线字体或将线条颜色设置为灰色。
如何对这些属性进行数据绑定?我是否需要用另一个不同的查询/命令/数据表重复整个过程...?
我想你想要这样的东西:
private void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView.Columns[e.ColumnIndex].Name == "Availability")
e.CellStyle.ForeColor = Color.Silver;
}
这基本上是将 Availability
列中所有单元格的文本涂成灰色。您可以扩展代码,只是为了演示这个想法。
此外:当然,您绝对应该在 SQL 中包含额外的列。使用 DataGridView
来控制它们的外观。您也可以完全隐藏这些列。