Winforms DataGrid默认值 [英] Winforms datagrid default values

查看:54
本文介绍了Winforms DataGrid默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯:

我正在使用datagridview(绑定到数据集)和文本框绑定到数据集(这意味着键入文本框的所有内容都会反映在datagridview中)。 datagridview实际上仅用于显示值(用户无法编辑单元格中的任何内容)。

i'm using a datagridview (bound to a dataset) and textboxes also bound to the dataset (which means that everything typed into the textboxes is reflected in the datagridview). The datagridview is actually used only to show up the values (user is NOT able to edit anything in the cells).

因此:当用户按下添加新记录按钮时(由Visual Studio使用绑定导航器等自动创建。)我想以编程方式选择此新行(请记住:用户无法使用鼠标进行选择)并插入一些值。

So: when the user pushes the add new record button (automatically created by visual studio using binding navigator etc. ) i would like to programmatically select this new line (remember: user has not the ability to select with the mouse) and insert some values.

我尝试使用

DefaultValuesNeeded 

事件,但这仅在用户选择带有星号指示符的行时才触发(不允许用户执行此操作)。

event, but this is firing only if the user selects the row with the asterisk indicator (what the user is not allowed to do).

我如何模拟这种行为?我应该采取另一种方法吗?

How can i simulate that behavior? Should i take another approach?

谢谢!

推荐答案

正在寻找我目前的方式的替代方法,只是想让您了解我所知道的几种方法。当使用*(新行)或使用BindingNavigator时,这两个都将设置默认值。

Was searching for alternatives to how I currently do this and just wanted to let you know about a couple ways I know of. Both of these will set defaults when using the * (new row) or using a BindingNavigator.

private void CityBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
    // Simply set the default value to the next viable ID, will automatically set the value when needed.
    locationDataSet.CITY.CITY_IDColumn.DefaultValue = CityTableAdapter.GetNewID(); // ++maxID;
}

private void CityDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
    // Gets called when datagridview's */NewRow is entered.
    e.Row.Cells[0].Value = CityTableAdapter.GetNewID(); // ++maxID;
}

private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
    // BindingNavigator button's AddNewItem automatic tie-in to bindingSource's AddNew() is removed
    // This sets the current cell to the NewRow's cell to trigger DefaultValuesNeeded event
    CityDataGridView.CurrentCell = CityDataGridView.Rows[CityDataGridView.NewRowIndex].Cells[1];
}

我真的很想用这个...

I was really hoping to use this...

private void CityBindingSource_AddingNew(object sender, AddingNewEventArgs e)
{
    // Cast your strongly-typed bindingsource List to a DataView and add a new DataRowView 
    DataRowView rowView = ((DataView)CityBindingSource.List).AddNew();
    var newRow = (LocationDTSDataSet.CITYRow)rowView.Row;
    newRow.CITY_ID = CityTableAdapter.GetNewID();
    newRow.CMMT_TXT = "Whatever defaults I want";
    e.NewObject = rowView;
}

但是使用BindingNavigator或任何基于代码的bindingSource.AddNew()添加时,结果只是一个空白行。希望有人能找到更好的方法,但这到目前为止对我有用。

But when adding using the BindingNavigator or any code based bindingSource.AddNew() it results in just a blank row. Hopefully someone finds a better way, but this is working for me so far.

这篇关于Winforms DataGrid默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆