DataGridViewColumn的属性SortMode决定了列的排序方式,将其设置为DataGridViewColumnSortMode.NotSortable就可以避免默认的排序行为。
25. 如何在点击工具栏按钮的时候将数据提交到?
this.Validate();
this.customersBindingSource.EndEdit(); this.customersTableAdapter.Update(this.northwindDataSet.Customers);
26. 如何在用户删除记录时显示确认对话框?
当用户选择DataGridView的一行,按下Delete键时就会触发UserDeletingRow 事件。你可以提示用户是否确定要删除该行记录,建议仅在用户要删除已存在的记录(而不是用户添加的新行)时才进行这种提示。将下面这些代码添加到UserDeletingRow事件的处理方法中就可以实现这种功能:
if (!e.Row.IsNewRow)
{
DialogResult response = MessageBox.Show("Are you sure?", "Delete row?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (response == DialogResult.No)
e.Cancel = true;
}
-------------
//针对某列的数据验证
if ((e.ColumnIndex ==3)
{
if (dgvStandard.IsCurrentCellDirty)
{
int iValue = 0;
if (!int.TryParse(e.FormattedValue.ToString(), out iValue))
{
DataGridViewCell cell = dgvStandard[e.ColumnIndex, e.RowIndex];
cell.ErrorText = "请输入数字";
// increase padding for icon. This moves the editing control
if (cell.Tag == null)
{
cell.Tag = cell.Style.Padding;
cell.Style.Padding = new Padding(0, 0, 18, 0);
cellInError = new Point(e.ColumnIndex, e.RowIndex);
}
if (errorTooltip == null)
{
errorTooltip = new ToolTip();
errorTooltip.InitialDelay = 0;
errorTooltip.ReshowDelay = 0;
errorTooltip.Active = false;
}
e.Cancel = true;
//取消更改
dgvStandard.CancelEdit();
}
else
{
dgvStandard[e.ColumnIndex, e.RowIndex].ErrorText = "";
}
}
}
-------数据异常时不抛出-----
/// <summary>
/// 数据异常时不抛出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgvStandard_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.ThrowException = false;
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-50070-65.html
不是假400那拨人自己成立的呢