我知道这是一个奇怪的问题,但我无法弄清楚为什么我的代码会按照我的意愿行事。
这就是我将数据放入datagridview
using (SqlConnection SqlCon = new SqlConnection(StrCon))
{
SqlCon.Open();
using (SqlCommand SqlCmd = new SqlCommand())
{
SqlCmd.Connection = SqlCon;
SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.CommandText = "SelectWe...";
SqlCmd.Parameters.Add("@HiveNumber", SqlDbType.Int).Value = ...;
SqlCmd.Parameters.Add("@RV", SqlDbType.Int).Direction = ...;
DataTable Dtable = new DataTable();
Dtable.Load(SqlCmd.ExecuteReader());
WeeklyRptdgv.Columns[0].DataPropertyName = "RecID";
WeeklyRptdgv.Columns[2].DataPropertyName = "VisitDate";
WeeklyRptdgv.Columns[3].Name = "QueenExists";
WeeklyRptdgv.Columns[3].DataPropertyName = "QueenExists";
WeeklyRptdgv.Columns[4].Name = "QueenVisited";
WeeklyRptdgv.Columns[4].DataPropertyName = "QueenVisited";
WeeklyRptdgv.Columns[5].Name = "QueenCellVisited";
WeeklyRptdgv.Columns[5].DataPropertyName = "QueenCellVisited";
WeeklyRptdgv.Columns[6].DataPropertyName = "HoneyStatus";
WeeklyRptdgv.Columns[7].DataPropertyName = "EggLayingStatus";
WeeklyRptdgv.DataSource = Dtable;
直到这里没关系,但3,4,5的列号是bool,我希望它们显示在CheckBox
而不是真/假。
所以我在设置数据源后执行此操作:
using (SqlConnection SqlCon = new SqlConnection(StrCon))
{
SqlCon.Open();
using (SqlCommand SqlCmd = new SqlCommand())
{
...
WeeklyRptdgv.DataSource = Dtable;
WeeklyRptdgv.Columns.Remove("QueenExists");
DataGridViewCheckBoxColumn QueenExists = new DataGridViewCheckBoxColumn();
QueenExists.Name = "QueenExists";
QueenExists.DataPropertyName = "QueenExists";
WeeklyRptdgv.Columns.Insert(3, QueenExists);
WeeklyRptdgv.Columns.Remove("QueenVisited");
DataGridViewCheckBoxColumn QueenVisited = new DataGridViewCheckBoxColumn();
QueenVisited.Name = "QueenVisited";
QueenVisited.DataPropertyName = "QueenVisited";
WeeklyRptdgv.Columns.Insert(4, QueenVisited);
WeeklyRptdgv.Columns.Remove("QueenCellVisited");
DataGridViewCheckBoxColumn QueenCellVisited = new DataGridViewCheckBoxColumn();
QueenCellVisited.Name = "QueenCellVisited";
QueenCellVisited.DataPropertyName = "QueenCellVisited";
WeeklyRptdgv.Columns.Insert(5, QueenCellVisited);
}
}
此代码执行我想要的操作但我无法找到列在原始列移除后如何获取其值
或者这个解决方案可能不对,我也不知道。