数据网格视图未显示数据。 C# [英] Data Grid View is not showing data. C#

查看:100
本文介绍了数据网格视图未显示数据。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写程序,其中文本框中的数据应该显示在数据网格视图中。当用户单击确定按钮时,buyitem会增加并且数据会传递给datagridview,但不会显示结果,而是会出错。



I am making program in which the data in textbox should be shown in data grid view.when a user click ok button buyitem is incremented and the data is passed to datagridview but instead of showing result it gives error.

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index




 public Form1()
        {
            InitializeComponent();
        }
        int buyItem = 0;
        connection con = new connection();

  private void btnOk_Click(object sender, EventArgs e)
        {


            dataGridView1.Rows[buyItem].Cells[0].Value = buyItem + 1;
            dataGridView1.Rows[buyItem].Cells[1].Value = txtItemno.Text;
            dataGridView1.Rows[buyItem].Cells[2].Value = cbxItem.Text;
            dataGridView1.Rows[buyItem].Cells[3].Value = cbxBrand.Text;
            dataGridView1.Rows[buyItem].Cells[4].Value = txtQty.Text;
            dataGridView1.Rows[buyItem].Cells[5].Value = txtPrice.Text;
            dataGridView1.Rows[buyItem].Cells[6].Value = txtPoint.Text;
            dataGridView1.Rows[buyItem].Cells[7].Value =txtLineTotal.Text;

            buyItem++;
}

推荐答案

我们无法为您解决这个问题 - 这取决于我们所拥有的大量信息;任何访问:您的DataGridView,您的数据等。



但您可以:在调试器中运行您的代码并仔细查看它停在的行。 />
查看变量:buyItem的值是多少? DGV有多少行?它有足够的行,每行有多少个单元格?



最有可能的是,DVG没有任何行,在这种情况下你需要创建一个新的DateGridViewRow并将其添加到DGV ...但只有你可以告诉我们,我们无法访问你正在运行的代码...
We can't solve that for you - it depends on a load of information we don;t have any access to: your DataGridView, your data, etc.

But you can: run your code in the debugger and look closely at the line it stops on.
Look at the variables: what is the value of buyItem? How many Rows does the DGV have? It it has sufficient rows, how many Cells does each Row have?

Most likely, the DVG doesn't have any rows, in which case you need to create a new DateGridViewRow and add it to the DGV...but only you can tell, we can't access your running code...


首先你应该在你的数据库中插入信息之后你必须从数据库中读取你的所有信息(通过存储过程并从列表中获取帮助)。最后用选定的项目填充你的gridview。(你应该用For来填充gridview的所有行)
First of all you should insert the information in your database.After it you must read all of your information from DB(by Store Procedure and get help from List).Finally fill your gridview with selected items.(you should use For to fill all rows of gridview)

我调试了它,它在
this line.

dataGridView1.Rows[buyItem].Cells[0].Value = buyItem + 1;

列数相同。


这篇关于数据网格视图未显示数据。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆