C# Datagridview返回列中的值(到字符串?)

C# Datagridview返回列中的值(到字符串?),c#,datagridview,C#,Datagridview,我试图将DataGridView值传递到字符串中,然后传递到文本框中。但我不明白我错在哪里 int CFee = 0; int CLAIMAMT = 0; int requests = 0; for (int i = 0; i < dataGridView1.Rows.Count; i++) { string requests = (dataGridView1.RowCount); while (requests.Length < 5) reques

我试图将DataGridView值传递到字符串中,然后传递到文本框中。但我不明白我错在哪里

int CFee = 0;
int CLAIMAMT = 0;
int requests = 0;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    string requests = (dataGridView1.RowCount);
    while (requests.Length < 5)
        requests = "0" + requests;
    textBox2.Text = requests.ToString();
    string  CFee = (dataGridView1.Rows[i].Cells["CFee"].Value);
    for (int i = 0; i < 5; i++)
        CFee = "0" + CFee;
    while (CFee.Length < 9)
        CFee += "0";
    textBox3.Text = CFee.ToString();
    string  CLAIMAMT = (dataGridView1.Rows[i].Cells["CLAIMAMT"].Value);
    for (int i = 0; i < 5; i++)
        CLAIMAMT = "0" + CLAIMAMT;
    while (CLAIMAMT.Length < 10)
        CLAIMAMT += "0";
    textBox4.Text = CLAIMAMT.ToString();
}
如果我通常给字符串赋值,比如string
requests=“2”
,我会得到它。但我想知道如何将DataGridView的值作为字符串传递。我得到一个错误,如:

A local variable named 'CFee' cannot be declared in this scope because it would give a different meaning to 'CFee', which is already used in a 'parent or current' scope to denote something else.

string CFee
重命名为
string strCFee
-这与
int CFee

冲突您需要重命名所有字符串变量,因为即使是不同类型的变量也不能具有相同的名称,否则编译器将不知道该怎么做

要使用Length,必须首先使用ToString()转换所需的内容

关于计数器变量“i”,它们与第一个for循环中已经声明的“i”相冲突。给他们取个名字就行了

还有一件事:要获取datagridview值,需要在将其分配给字符串变量之前:

string  strCLAIMAMT = dataGridView1.Rows[i].Cells["CLAIMAMT"].Value.ToString();

谢谢你。。如果我重命名,我会遇到一个错误,如
'int'不包含'Length'的定义,并且找不到接受第一个'int'类型参数的扩展方法'Length'(是否缺少using指令或程序集引用?)
您在
string clairmt
中面临同样的问题-将其重命名为
string strcaimamt
,然后使用
strcaimamt.Length
而不是
claimmt.Length
-对
请求执行相同的操作。我尝试更改所有内容,但仍然出错。。弗鲁塔克梅。。。请帮帮我
无法在此范围内声明名为“i”的局部变量,因为它将赋予“i”不同的含义,而“i”已在“父级或当前”范围内用于表示其他内容
@preethi-为什么您在世界各地都使用相同的变量名?尝试此操作-无法将类型“object”隐式转换为“string”。存在显式转换(是否缺少强制转换?)您希望从发布的代码中实现什么确切功能?我希望获得所需的输出。我尝试了各种方法,但运气不佳。我尝试了上述代码,它非常完美,但无法将datagrid视图值传递给string-like<代码>字符串请求=(dataGridView1.RowCount)确定。让我们让理解变得容易。对于第0行,假设dataGridView1.Rows[i].Cells[“CFee”]。值为1,那么textBox3的值是多少?
string  strCLAIMAMT = dataGridView1.Rows[i].Cells["CLAIMAMT"].Value.ToString();