Datagridview返回列中的值(To String?)

时间:2014-02-27 13:29:10

标签: c# datagridview

我试图将DataGridView值传递给字符串,然后传递给TextBox。但我不明白我哪里错了。

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();
}

我需要的输出是:

no of requests = 00002; // 5 characters
court fee = 000006000; // 9 characters
claim amount = 0000020000; // 10 characters 

如果我通常将字符串赋值给字符串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.

2 个答案:

答案 0 :(得分:0)

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

冲突

答案 1 :(得分:0)

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

并且,要使用Length,您必须首先使用ToString()来转换您想要的内容。

关于计数器变量'i',它们与第一个for循环中已声明的'i'冲突。只要给他们另一个名字。

还有另一件事:在将数据格式视图值分配给字符串变量之前,需要{} convert to string获取数据网格值:

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