我在 Windows 窗体类(称为 DataGridView
)中有一个 SummaryDataGrid
(称为 SummaryForm
),我试图通过 ComboBox
下拉选择以编程方式更新(称为 YearComboBox)。 SummaryDataGrid
用户不可编辑,它仅用于以类似电子表格的方式显示信息。我希望能够使用 YearComboBox
让用户选择年份,然后使用 SummaryDataGrid
根据该年份选择更新其值。您可以在下面的链接中看到 SummaryForm
的屏幕截图,其中显示了 2020 年填充的 SummaryDataGrid
。
YearComboBox
不是数据绑定的,SummaryDataGrid
也不是。为了以编程方式填充 SummaryDataGrid
,我通过在 DataGridViewRows
中创建 SummaryForm
然后使用 MainForm
中定义的函数从我的 SQL 数据库中提取数据来填充数据,反过来使用链接到我托管的 SQL Server
的数据集中定义的函数。一些代码示例可以在下面找到。
public partial class SummaryForm : Form
{
private DataGridViewRow JanuaryRow = new DataGridViewRow();
private DataGridViewRow FebruaryRow = new DataGridViewRow();
private DataGridViewRow MarchRow = new DataGridViewRow();
private DataGridViewRow AprilRow = new DataGridViewRow();
private DataGridViewRow MayRow = new DataGridViewRow();
private DataGridViewRow JuneRow = new DataGridViewRow();
private DataGridViewRow JulyRow = new DataGridViewRow();
private DataGridViewRow AugustRow = new DataGridViewRow();
private DataGridViewRow SeptemberRow = new DataGridViewRow();
private DataGridViewRow OctoberRow = new DataGridViewRow();
private DataGridViewRow NovemberRow = new DataGridViewRow();
private DataGridViewRow DecemberRow = new DataGridViewRow();
private DataGridViewRow TotalRow = new DataGridViewRow();
private DataGridViewRow AverageRow = new DataGridViewRow();
private int year = DateTime.Today.Year;
...
// form load event handler
private void SummaryForm_FormLoad(Object sender, EventArgs e)
{
YearComboBox.Text = year.ToString();
JanuaryRow.CreateCells(SummaryDataGrid);
FebruaryRow.CreateCells(SummaryDataGrid);
MarchRow.CreateCells(SummaryDataGrid);
AprilRow.CreateCells(SummaryDataGrid);
MayRow.CreateCells(SummaryDataGrid);
JuneRow.CreateCells(SummaryDataGrid);
JulyRow.CreateCells(SummaryDataGrid);
AugustRow.CreateCells(SummaryDataGrid);
SeptemberRow.CreateCells(SummaryDataGrid);
OctoberRow.CreateCells(SummaryDataGrid);
NovemberRow.CreateCells(SummaryDataGrid);
DecemberRow.CreateCells(SummaryDataGrid);
TotalRow.CreateCells(SummaryDataGrid);
AverageRow.CreateCells(SummaryDataGrid);
// populate january row
JanuaryRow.Cells[0].Value = "January";
JanuaryRow.Cells[1].Value = MainFormInstance.BillsOfSaleCountMonth(1, year);
JanuaryRow.Cells[2].Value = MainFormInstance.BillsOfSaleCashMonth(1, year);
JanuaryRow.Cells[3].Value = MainFormInstance.BillsOfSaleCreditMonth(1, year);
JanuaryRow.Cells[4].Value = Convert.ToDecimal(JanuaryRow.Cells[2].Value) + Convert.ToDecimal(JanuaryRow.Cells[3].Value);
JanuaryRow.Cells[5].Value = MainFormInstance.CashMonth(1, year);
JanuaryRow.Cells[6].Value = MainFormInstance.CardMonth(1, year);
JanuaryRow.Cells[7].Value = MainFormInstance.MonthCP(1, year);
JanuaryRow.Cells[8].Value = MainFormInstance.ExpensesMonthCP(1, year) + MainFormInstance.BillsOfSaleCashMonth(1, year);
JanuaryRow.Cells[9].Value = Convert.ToDecimal(JanuaryRow.Cells[7].Value) - Convert.ToDecimal(JanuaryRow.Cells[8].Value);
JanuaryRow.Cells[10].Value = MainFormInstance.MonthCF(1, year);
JanuaryRow.Cells[11].Value = MainFormInstance.ExpensesMonthCF(1, year);
JanuaryRow.Cells[12].Value = Convert.ToDecimal(JanuaryRow.Cells[10].Value) - Convert.ToDecimal(JanuaryRow.Cells[11].Value);
JanuaryRow.Cells[13].Value = MainFormInstance.ExpensesMonthGeneral(1, year);
JanuaryRow.Cells[14].Value = Convert.ToDecimal(JanuaryRow.Cells[5].Value) + Convert.ToDecimal(JanuaryRow.Cells[6].Value);
JanuaryRow.Cells[15].Value = Convert.ToDecimal(JanuaryRow.Cells[8].Value) + Convert.ToDecimal(JanuaryRow.Cells[11].Value) + Convert.ToDecimal(JanuaryRow.Cells[13].Value);
JanuaryRow.Cells[16].Value = Convert.ToDecimal(JanuaryRow.Cells[14].Value) - Convert.ToDecimal(JanuaryRow.Cells[15].Value);
SummaryDataGrid.Rows.Add(JanuaryRow);
...
}
// year combo box selection event handler
private void SummaryForm_YearChanged(Object sender, EventArgs e)
{
year = int.Parse(YearComboBox.Text);
PopulateSummaryDataGrid();
}
// populate the summary data grid
private void PopulateSummaryDataGrid()
{
// stubbed
}
我为 YearComboBox
定义了一个 SelectedValueChanged 事件处理程序,并且最初尝试将每一行的填充移动到与 FormLoad
事件处理程序不同的函数中。这个想法是让 FormLoad
事件处理程序从 SummaryForm 中定义的年份变量更新 YearComboBox 初始 Text 属性,为每一行执行 CreateCells()
调用,然后调用单独的 void 函数来执行填充.但是,在调试时,单独的函数会立即到达尝试编辑单元格的第一个 JanRow 语句,并会说索引为 -1,因此超出范围。我尝试让 SelectedValueChanged 事件触发 FormLoad
事件处理程序,但由于行已被创建和填充,这也不起作用。
所以,我的问题是我怎样才能获得这个下拉选择来更改 FormLoad
事件处理程序中的语句使用的年份(工作正常)并让它用新值重新填充 DataGridView基于更新的年份?
答案 0 :(得分:0)
好吧,我似乎在尝试不同的事情时偶然发现了一个解决方案。可能不是最优雅的,但这是我对 PopulateSummaryGrid() 的实现,以防其他人遇到此问题:
// populate the summary data grid
private void PopulateSummaryDataGrid()
{
// update January row
SummaryDataGrid.Rows[0].Cells[1].Value = MainFormInstance.BillsOfSaleCountMonth(1, year);
SummaryDataGrid.Rows[0].Cells[2].Value = MainFormInstance.BillsOfSaleCashMonth(1, year);
SummaryDataGrid.Rows[0].Cells[3].Value = MainFormInstance.BillsOfSaleCreditMonth(1, year);
SummaryDataGrid.Rows[0].Cells[4].Value = Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[2].Value) + Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[3].Value);
SummaryDataGrid.Rows[0].Cells[5].Value = MainFormInstance.CashMonth(1, year);
SummaryDataGrid.Rows[0].Cells[6].Value = MainFormInstance.CardMonth(1, year);
SummaryDataGrid.Rows[0].Cells[7].Value = MainFormInstance.MonthCP(1, year);
SummaryDataGrid.Rows[0].Cells[8].Value = MainFormInstance.ExpensesMonthCP(1, year) + MainFormInstance.BillsOfSaleCashMonth(1, year);
SummaryDataGrid.Rows[0].Cells[9].Value = Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[7].Value) - Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[8].Value);
SummaryDataGrid.Rows[0].Cells[10].Value = MainFormInstance.MonthCF(1, year);
SummaryDataGrid.Rows[0].Cells[11].Value = MainFormInstance.ExpensesMonthCF(1, year);
SummaryDataGrid.Rows[0].Cells[12].Value = Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[10].Value) - Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[11].Value);
SummaryDataGrid.Rows[0].Cells[13].Value = MainFormInstance.ExpensesMonthGeneral(1, year);
SummaryDataGrid.Rows[0].Cells[14].Value = Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[5].Value) + Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[6].Value);
SummaryDataGrid.Rows[0].Cells[15].Value = Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[8].Value) + Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[11].Value) + Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[13].Value);
SummaryDataGrid.Rows[0].Cells[16].Value = Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[14].Value) - Convert.ToDecimal(SummaryDataGrid.Rows[0].Cells[15].Value);
...
SummaryDataGrid.Refresh();
}
我还必须删除 FormLoad 事件处理程序的第一条语句,该语句将 YearComboBox 的文本设置为当前年份。这似乎一切正常。