我想请你帮忙解决我的问题。我有一个DGV,其中每列都使用List<>中的DataPropertyName设置。当我装载它时,DGV充满了Ids的班次。它工作得很好,但是我想把这个(带有int的单元格)更改为ComboBox,其中displaymember将是shift名称和值成员这个班次ID。
有人有任何解决方案或者我应该重拍吗?
非常感谢
这是我的代码
public void FillWithData(int month, int year, int centerID)
{
IScheduleRecord record = new ScheduleRecordDAO();
List<Shift> shifts = new ShiftDAO().GetShiftsByCenterId(center);
dataGridView1.Columns[1].DataPropertyName = "Day1";
dataGridView1.Columns[2].DataPropertyName = "Day2";
dataGridView1.Columns[3].DataPropertyName = "Day3";
dataGridView1.Columns[4].DataPropertyName = "Day4";
dataGridView1.Columns[5].DataPropertyName = "Day5";
dataGridView1.Columns[6].DataPropertyName = "Day6";
dataGridView1.Columns[7].DataPropertyName = "Day7";
dataGridView1.Columns[8].DataPropertyName = "Day8";
dataGridView1.Columns[9].DataPropertyName = "Day9";
dataGridView1.Columns[10].DataPropertyName = "Day10";
dataGridView1.Columns[11].DataPropertyName = "Day11";
dataGridView1.Columns[12].DataPropertyName = "Day12";
dataGridView1.Columns[13].DataPropertyName = "Day13";
dataGridView1.Columns[14].DataPropertyName = "Day14";
dataGridView1.Columns[15].DataPropertyName = "Day15";
dataGridView1.Columns[16].DataPropertyName = "Day16";
dataGridView1.Columns[17].DataPropertyName = "Day17";
dataGridView1.Columns[18].DataPropertyName = "Day18";
dataGridView1.Columns[19].DataPropertyName = "Day19";
dataGridView1.Columns[20].DataPropertyName = "Day20";
dataGridView1.Columns[21].DataPropertyName = "Day21";
dataGridView1.Columns[22].DataPropertyName = "Day22";
dataGridView1.Columns[23].DataPropertyName = "Day23";
dataGridView1.Columns[24].DataPropertyName = "Day24";
dataGridView1.Columns[25].DataPropertyName = "Day25";
dataGridView1.Columns[26].DataPropertyName = "Day26";
dataGridView1.Columns[27].DataPropertyName = "Day27";
dataGridView1.Columns[28].DataPropertyName = "Day28";
if (CountColumns(month, year) == 30) {
dataGridView1.Columns[29].DataPropertyName = "Day29";
dataGridView1.Columns[30].DataPropertyName = "Day30";
}
if (CountColumns(month, year) == 31) {
dataGridView1.Columns[29].DataPropertyName = "Day29";
dataGridView1.Columns[30].DataPropertyName = "Day30";
dataGridView1.Columns[31].DataPropertyName = "Day31";
}
dataGridView1.DataSource = record.GetScheduleRecordsByMonthYearCenter(month, year, center);
this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Beige;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
Color.LightGray;
this.dataGridView1.Columns["Year"].Visible = false;
this.dataGridView1.Columns["Month"].Visible = false;
this.dataGridView1.Columns["CenterID"].Visible = false;
this.dataGridView1.Columns["Id"].Visible = false;
this.dataGridView1.Columns["EmployeeID"].Visible = false;
if (CountColumns(month, year) == 28)
{
this.dataGridView1.Columns["Day29"].Visible = false;
this.dataGridView1.Columns["Day30"].Visible = false;
this.dataGridView1.Columns["Day31"].Visible = false;
}
if (CountColumns(month, year) == 30)
{
this.dataGridView1.Columns["Day31"].Visible = false;
}
答案 0 :(得分:0)
我在我的一个应用程序中使用辅助方法来添加DataGridViewComboBoxColumn控件。希望它可以帮助您到达您需要的地方:
private void AddBoundDataGridViewComboBoxColumn(
DataGridView dgv,
string dataPropertyName,
string displayMember,
string valueMember,
object dataSource,
string headerText,
string toolTipText = "")
{
DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();
int columnIndex;
if (ts.TraceVerbose) Trace.WriteLine("entering");
try
{
#region Try
comboBoxColumn.DataPropertyName = dataPropertyName;
comboBoxColumn.Name = dataPropertyName;
comboBoxColumn.DisplayMember = displayMember;
comboBoxColumn.ValueMember = valueMember;
comboBoxColumn.DataSource = dataSource;
columnIndex = dgv.Columns.Add(comboBoxColumn);
dgv.Columns[columnIndex].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dgv.Columns[columnIndex].HeaderText = headerText;
dgv.Columns[columnIndex].ToolTipText = toolTipText;
if (ts.TraceVerbose) Trace.WriteLine("Column added to " + dgv.Name + ": " + dataPropertyName + " (Tool tip: " + toolTipText + ")");
#endregion Try
}
catch (Exception)
{
throw;
}
finally
{
if (ts.TraceVerbose) Trace.WriteLine("exiting");
}
}
然后我使用通用事件处理程序进行单元格内容点击:
private void dgvSCXTableView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex >= 0)
{
}
}
希望它对你有所帮助。如果您在以下行中发布Shift的实现可能有所帮助:
List<Shift> shifts = new ShiftDAO().GetShiftsByCenterId(center);