简体   繁体   English

C# 从 DataGridView 将 Double 转换为字符串

[英]C# Convert Double to a string from a DataGridView

On my button click I am getting the following error when the value being replaced is only a number (eg 40 ) and it is being replaced by a string (eg AB123 ).在我的按钮单击上,当被替换的值只是一个数字(例如40 )并且它被一个字符串(例如AB123 )替换时,我收到以下错误。

System.Exception: AB123 is not a valid value for Double. System.Exception:AB123 不是 Double 的有效值。 ---> --->

System.FormatException: Input string was not in a correct format. System.FormatException: 输入字符串的格式不正确。

at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)在 System.Number.ParseDouble(字符串值,NumberStyles 选项,NumberFormatInfo numfmt)

Button Click:按钮点击:

    private void button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            if (!RowIsEmpty(i))
            {
                dataGridView1[3, i].Value = Combo.Text;
            }
        }
    }

I have tried implicitly converting this like so:我试过像这样隐式转换这个:

dataGridView1[3, i].Value.ToString() = Combo.Text;

But that doesn't work, I have also tried calling it as a string like so:但这不起作用,我也尝试将其称为字符串,如下所示:

    private void button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            if (!RowIsEmpty(i))
            {
                string DGVS = dataGridView1[3, i].Value.ToString();
                DGVS =  Combo.Text;
            }
        }
    }

But that doesn't give an error but doesn't actually do anything.但这不会出错,但实际上并没有做任何事情。

Data is loaded in from an excel file.数据从 Excel 文件加载。

Example例子

Col1  Col2  Col3
AAA   BBB   40

ComboBox:组合框:

ABC123

Ok I guess I found the solution:好的,我想我找到了解决方案:

double price;
bool isDouble = Double.TryParse(textBox1.Text, out price);
if (isDouble)
{
     Convert.ToDouble(textBox1.Text);
     dataGridView1[0, 0].Value = textBox1.Text;
}
else
{
     dataGridView1[0, 0].Value = textBox1.Text;
}

Basically, first check if the textbox is double or not.基本上,首先检查文本框是否是双重的。 If is double, convert to double and add it to DGV.如果是 double,则转换为 double 并将其添加到 DGV。 If it's string, just add the string into DGV.如果是字符串,只需将字符串添加到 DGV 中即可。

Tests:测试:

TextBox1.Text = AAA; //worked
TextBox1.Text = BBB; //worked
TextBox1.Text = 40; //worked
TextBox1.Text = 40.00; //worked
TextBox1.Text = 42,20; //worked

I resolved this (maybe not the best way but it works) by removing and re adding the column after the load into the datagridview but before the update of the combobox value into the rows.我通过在加载到 datagridview 之后但在将组合框值更新到行之前删除并重新添加列来解决这个问题(可能不是最好的方法,但它有效)。

private void button2_Click(object sender, EventArgs e)
{
    DataGridViewTextBoxColumn Column3New = new DataGridViewTextBoxColumn();
    Column3New.Name = "Column3";
    Column3New.HeaderText = "Column3";
    dataGridView1.Columns.RemoveAt(3);
    dataGridView1.Columns.Insert(3, Column3New);
    for (int i = 0; i < dataGridView1.RowCount; i++)
    {
        if (!RowIsEmpty(i))
        {
            dataGridView1[3, i].Value = Combo.Text;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM