DataGridViewCell .EditedFormattedValue 属性
如果该单元格处于编辑模式,则此属性返回该单元格或编辑控件的当前值。否则,获取此属性的值会将单元格值转换为由 FormattedValueType 属性所指示类型的等效显示值。 这将引发 DataGridView .CellFormatting 事件,您可以通过处理该事件来自定义值的转换。
如果格式化失败,则将发生 DataGridView .DataError 事件。 如果此事件无处理程序或处理程序将 DataGridViewDataErrorEventArgs .ThrowException 属性设置为true,则会引发异常。
下面的代码示例演示如何使用 EditedFormattedValue 属性。 在本示例中, IsCurrentCellDirty 属性用于确定是否已编辑但并未提交当前单元格的内容。如果该单元格已经过修改,则使用编辑后的值。 此示例摘自 DataGridView .SelectionChanged 事件中提供的一个更大的示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
private void UpdateLabelText() { int WithdrawalTotal = 0; int DepositTotal = 0; int SelectedCellTotal = 0; int counter; // Iterate through all the rows and sum up the appropriate columns. for (counter = 0; counter < (DataGridView1.Rows.Count); counter++) { if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value != null) { if (DataGridView1.Rows[counter]. Cells["Withdrawals"].Value.ToString().Length != 0) { WithdrawalTotal += int.Parse(DataGridView1.Rows[counter]. Cells["Withdrawals"].Value.ToString()); } } if (DataGridView1.Rows[counter].Cells["Deposits"].Value != null) { if (DataGridView1.Rows[counter] .Cells["Deposits"].Value.ToString().Length != 0) { DepositTotal += int.Parse(DataGridView1.Rows[counter] .Cells["Deposits"].Value.ToString()); } } } // Iterate through the SelectedCells collection and sum up the values. for (counter = 0; counter < (DataGridView1.SelectedCells.Count); counter++) { if (DataGridView1.SelectedCells[counter].FormattedValueType == Type.GetType("System.String")) { string value = null; // If the cell contains a value that has not been commited, // use the modified value. if (DataGridView1.IsCurrentCellDirty == true) { value = DataGridView1.SelectedCells[counter] .EditedFormattedValue.ToString(); } else { value = DataGridView1.SelectedCells[counter] .FormattedValue.ToString(); } if (value != null) { // Ignore cells in the Description column. if (DataGridView1.SelectedCells[counter].ColumnIndex != DataGridView1.Columns["Description"].Index) { if (value.Length != 0) { SelectedCellTotal += int.Parse(value); } } } } } // Set the labels to reflect the current state of the DataGridView. Label1.Text = "Withdrawals Total: " + WithdrawalTotal.ToString(); Label2.Text = "Deposits Total: " + DepositTotal.ToString(); Label3.Text = "Selected Cells Total: " + SelectedCellTotal.ToString(); Label4.Text = "Total entries: " + DataGridView1.RowCount.ToString(); } |
C# Winform中DataGridView的DataGridViewCheckBoxColumn使用方法
Winform中DataGridView的DataGridViewCheckBoxColumn使用方法:
DataGridViewCheckBoxColumn CheckBox是否选中
在判断DataGridView中CheckBox选中列的时候,用DataGridViewRow.Cells[0].FormattedValue.ToString()==”True”语句时存在问题,当我们直接点击CheckBox时,结果显示未选中,但是如果我们在点击其他单元格时,结果显示选中。而用DataGridViewRow.Cells[0].EditedFormattedValue.ToString()==”True”语句时不管怎么样是选中的状态。
为什么会有这种结果?
原因:就是FormattedValue是操作提交后的结果,而EditedFormattedValue是当前的结果,不管结果是否已经提交。
所以用DataGridViewRow.Cells[0].EditedFormattedValue.ToString()==”True”判断选中比较合适
1 2 3 4 5 6 7 8 9 |
if (dgvDownloadList.Rows.Count > 0) { for (int i = 0; i < dgvDownloadList.Rows.Count; i++) { string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString(); if (_selectValue == "True") //如果CheckBox已选中,则在此处继续编写代码 } } |
DataGridViewCheckBoxColumn 设置CheckBox默认选中
((DataGridViewCheckBoxCell)dgvDownloadList.Rows[i].Cells[“Column1”]).Value = true;
DataGridViewCheckBoxColumn 第一时间获取CheckBox的选中状态
当点击或者取消datagridview的checkbox列时,比较难获得其状态是选中还是未选中,进而不好进行其它操作,下面就列出它的解决办法:
CommitEdit :将当前单元格中的更改提交到数据缓存,但不结束编辑模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
dgvDownloadList.CurrentCellDirtyStateChanged += new EventHandler(dgvDownloadList_CurrentCellDirtyStateChanged); dgvDownloadList.CellValueChanged += new DataGridViewCellEventHandler(dgvDownloadList_CellValueChanged); void dgvDownloadList_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dgvDownloadList.IsCurrentCellDirty) { dgvDownloadList.CommitEdit(DataGridViewDataErrorContexts.Commit); } } void dgvDownloadList_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (dgvDownloadList.Rows.Count > 0) { for (int i = 0; i < dgvDownloadList.Rows.Count; i++) { string _selectValue = dgvDownloadList.Rows[i].Cells["Column1"].EditedFormattedValue.ToString(); if (_selectValue == "True") //如果CheckBox已选中,则在此处继续编写代码 } } } |