Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Win Form DataGridView C.NET中字符串的第一个字符设为大写_C#_.net_Datagridview - Fatal编程技术网

C# 将Win Form DataGridView C.NET中字符串的第一个字符设为大写

C# 将Win Form DataGridView C.NET中字符串的第一个字符设为大写,c#,.net,datagridview,C#,.net,Datagridview,因此,我在我的C Win表单应用程序中有一个数据网格视图,默认情况下单元格是可编辑的,现在我希望无论何时在这些单元格中键入内容,结束值都应首先作为大写字母处理,这意味着如果有用户键入: *string => String example => Example another => Another chaRactEr => ChaRactEr* 我可以在Cell Value Changed事件中的代码中执行此操作,但当我在Cell Value Changed事件中执行此操

因此,我在我的C Win表单应用程序中有一个数据网格视图,默认情况下单元格是可编辑的,现在我希望无论何时在这些单元格中键入内容,结束值都应首先作为大写字母处理,这意味着如果有用户键入:

*string => String
example => Example
another => Another
chaRactEr => ChaRactEr*
我可以在Cell Value Changed事件中的代码中执行此操作,但当我在Cell Value Changed事件中执行此操作并将该单元格的值设置为最终用户所需的格式化字符串时,事件会触发两次。我不能让这种情况发生,因为在此事件中会触发数据库功能。 我曾尝试在其他事件(如单元格离开、单元格进入和其他事件)中捕获单元格值,但始终无法捕获它

所以我需要知道,C.NET中的数据网格视图是否有任何属性或特征,使得值的第一个字符成为大写? 任何关于这一点的替代建议都会非常有用

您可以使用以下代码:

        bool bchange = false;
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (bchange == false)
            {
                bchange = true;
                String oritext = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                String newtext= oritext.First().ToString().ToUpper() + oritext.Substring (1);
                //Update Database

                //Update cell
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = newtext;

            }
            else 
            {
                bchange = false; 
            }
        }

DataGridView有一个事件“CellFormatting”。你可以选择这样的方式:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.Value != null)
        {
            string str = (string)e.Value;
            str = char.ToUpper(str[0]) + str.Substring(1);
            e.Value = str;
        }
    }

我只能猜测您可能忽略了,当代码“更改”调用CellValueChanged事件的“单元格”中的值时,当其值更改为大写字符串时,显然会再次“触发”CellValueChanged事件

为了避免这种循环引用,只需在更改单元格值之前关闭事件,更改单元格值…事件将不会触发…然后在值更改后重新打开事件

下面的示例:检查更改的单元格是否在第0列中,更改单元格中的字符串以使第一个字符成为大写字符。代码利用表单上的文本框,该文本框将包含指示触发CellValueChanged事件的时间的文本。如果代码以已发布的注释代码运行,则文本框将包含two第1列中的单元格值每次更改2个条目。取消注释这两行代码将显示文本框条目只有1个条目。将“更改”单元格值的代码行夹在关闭事件的代码行和将其重新打开的代码行之间。希望这有意义

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
  if (e.RowIndex >= 0 && e.ColumnIndex >= 0) {
    if (e.ColumnIndex == 0 && dataGridView1.Rows[e.RowIndex].Cells[0].Value != null) {
      string currentData = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
      string newData = char.ToUpper(currentData[0]) + currentData.Substring(1);
      //dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
      dataGridView1.Rows[e.RowIndex].Cells[0].Value = newData;
      //dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
      textBox3.Text += "CellValueChanged fired!" + Environment.NewLine;
    }
  }
}

当Cell Value Changed事件中的代码进行格式更改时,设置一个标志并跳过数据库代码。当出现第二个触发器时,检测标志,重置标志并执行数据库代码。另一个选项是,对正确的类进行子类化,以便始终首先使用大写字母设置值。下面是DataGridView的源代码:Have您试图在“更改明显再次触发它的值”之前将CellValueChanged事件“关闭”,在“更改”之后将其“打开”您是否更改了值?@JoelHarkes我试图不依赖某个标志。@JohnG您希望我通过某个bool标志来关闭/打开事件吗?或者是否有默认命令或代码来关闭/打开任何事件?我非常感谢您的回答。:-我试图不依赖某个标志。我的主要问题是,.NET w中是否有DataGridView的默认属性这将使单元格的第一个字符大写?DataGridView不存在此属性。您还可以使用:this.dataGridView1.CellValueChanged-=new System.Windows.Forms.DataGridViewCellEventHandlerthis.DataGridView1CellValueChanged;禁用CellValueChanged中的事件。this.dataGridView1.CellValueChanged+=new System.Windows.Forms。DataGridViewCellEventHandlerthis.dataGridView1\u CellValueChanged;要在CellDoubleClick中启用事件,请单击或…在CellValueChanged之前还是之后调用此函数?