简体   繁体   English

C#文本框到datagridview

[英]c# textbox to datagridview

I want to add the exact value of textbox into datagridview my problem is if i will add another item the last item I add will also change. 我想将textbox的确切值添加到datagridview我的问题是,如果我要添加另一个项目,那么我添加的最后一个项目也会更改。 Here is the print screen of sample problem.. 这是示例问题的打印屏幕。

1st try 第一次尝试

在此处输入图片说明

2nd try 第二次尝试

在此处输入图片说明

This is my code. 这是我的代码。

int n = dataGridView3.Rows.Add();

for (int j = 0; j < dataGridView3.RowCount; j++) 
{
    if (dataGridView3.Rows[j].Cells[1].Value != null && (textBox4.Text == dataGridView3.Rows[j].Cells[4].Value.ToString()))
    {
        MessageBox.Show("Item Already on List!");
        dataGridView3.Rows.Remove(dataGridView3.Rows[n]);
        return;
    }
    else 
    {
        dataGridView3.Rows[j].Cells[1].Value = textBox43.Text;
        dataGridView3.Rows[j].Cells[4].Value = textBox4.Text;
        dataGridView3.Rows[j].Cells[2].Value = DateTime.Now.ToShortDateString();
        dataGridView3.Rows[j].Cells[3].Value = dateTimePicker3.Text;            

        dataGridView3.FirstDisplayedScrollingRowIndex = n;
        dataGridView3.CurrentCell = dataGridView3.Rows[n].Cells[0];
        dataGridView3.Rows[n].Selected = true; 
    }
}

You are looping over the complete array and if it is not yet on the list it goes in to the else part of your if. 您正在遍历整个数组,如果尚未在列表中,它将进入if的else部分。 In that block you assign the current entered values to your row, for every single row you already have. 在该块中,对于您已经拥有的每一行,将当前输入的值分配给该行。

To fix that I separated the Check for duplicates and the Add part more clearly. 为了解决这个问题,我将检查重复项和添加部分更清楚地分开了。

Do notice that if you would have run this through the debugger and stepped on each line of your code (hitting F10 in Visual Studio) you would have spotted this bug easily. 请注意,如果您将通过调试器运行此代码并单步执行代码的每一行(在Visual Studio中达到F10),您将很容易发现此错误。 Have a look at the blog from Scott Guthrie (among others) http://weblogs.asp.net/scottgu/debugging-tips-with-visual-studio-2010 看看Scott Guthrie(除其他外)的博客http://weblogs.asp.net/scottgu/debugging-tips-with-visual-studio-2010

    // check if we already added that one
    for (int j = 0; j < dataGridView3.RowCount; j++) 
    {
        if (dataGridView3.Rows[j].Cells[1].Value != null && (textBox4.Text == dataGridView3.Rows[j].Cells[4].Value.ToString()))
        {
            MessageBox.Show("Item Already on List!");
            return;
        }
     }

     // lets add it!            
     int n = dataGridView3.Rows.Add();

     dataGridView3.Rows[n].Cells[1].Value = textBox43.Text;
     dataGridView3.Rows[n].Cells[4].Value = textBox4.Text;
     dataGridView3.Rows[n].Cells[2].Value = DateTime.Now.ToShortDateString();
     dataGridView3.Rows[n].Cells[3].Value = dateTimePicker3.Text;

     dataGridView3.FirstDisplayedScrollingRowIndex = n;
     dataGridView3.CurrentCell = dataGridView3.Rows[n].Cells[0];
     dataGridView3.Rows[n].Selected = true;

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

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