dataGridView - 网格不拉入数据 [英] dataGridView -- Grid not pulling in data

查看:88
本文介绍了dataGridView - 网格不拉入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的SQL语句中的数据没有进入网格,即使我可以看到返回了三条记录。我在另一个应用程序中使用这个代码块,它工作得很好,我可以拉出查询并在SSMS中运行它,看看这里要返回的三个记录。有人可以指出我做错了什么,我只是没有看到它。



错误信息:

System.ArgumentOutOfRangeExce

{索引超出范围。必须是非负数且小于集合的大小。\\\\ nParameter name:index}





The data from my SQL statement is not getting into the grid, even though I can see that three records are getting returned. I use this block of code in another application and it works just fine, I can pull the query out and run it in SSMS and see the three records that are to be returned here. Could someone please point out what I am doing wrong, I am just not seeing it.

Error Message:
System.ArgumentOutOfRangeExce
{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}


namespace BackFill_WinForm
{
    public partial class Form_FixReceiverLabel : Form
    {
        private BindingSource bindingSource1 = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        public string xConnection = ""

        private void GetData(string selectCommand)
        {
            // Connection string
            String connectionString = xConnection;

            // Create a new data adapter based on the specified query.
            dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

            //Create a command builder to generate SQL commands based on selectCommand.
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

            // Populate a new data table and bind it to the BindingSource.
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            bindingSource1.DataSource = table;

            // Resize the DataGridView columns to fit the newly loaded content.
            dataGridView2.AutoResizeColumns(
                DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            GridCount = dataGridView2.RowCount;

            if (this.dataGridView2.RowCount == 0)
            {
                MessageBox.Show("Error: No data found.");
                return;
            }

            dataGridView2.Columns[0].HeaderText = "Item Number";
            dataGridView2.Columns[0].Width = 50;

            dataGridView2.Columns[1].HeaderText = "Receiver";
            dataGridView2.Columns[1].Width = 80;

            dataGridView2.Columns[2].HeaderText = "Part Number";
            dataGridView2.Columns[2].Width = 200;

            dataGridView2.Columns[3].HeaderText = "Lot Number";
            dataGridView2.Columns[3].Width = 90;


            // Add a column for the select button . 
            DataGridViewButtonColumn buttonColumn =
                new DataGridViewButtonColumn();
            buttonColumn.HeaderText = "";
            buttonColumn.Name = "Status Request";
            buttonColumn.Text = "Select";
            buttonColumn.UseColumnTextForButtonValue = true;
            buttonColumn.DefaultCellStyle.BackColor = Color.DodgerBlue;
            buttonColumn.DefaultCellStyle.SelectionBackColor = Color.Yellow;

            dataGridView2.Columns.Add(buttonColumn);
        }

        private void GetReceiver()
        {
            GetData("SELECT " +
                    "  ISNULL(Rtrim(rcitem.fitemno),'') AS Item_number  " +
                    " ,ISNULL(Rtrim(rcitem.freceiver),'') AS Receiver " +
                    " ,ISNULL(Rtrim(rcitem.fpartno),'') AS Part_Number " +
                    " ,ISNULL(Rtrim(rclotc.fclot),'') AS Lot_number " +
                    " FROM  " + 
                    "  RCITEM_EXT  " +
                    "     INNER JOIN rcitem ON RCITEM_EXT.FKey_ID = rcitem.identity_column " + 
                    "     LEFT OUTER JOIN rcmast ON rcitem.freceiver = rcmast.freceiver " +
                    "     LEFT OUTER JOIN rclotc ON rcitem.freceiver + rcitem.fitemno  = fcrcitmkey " +
                    " 	LEFT OUTER JOIN inmastx ON rcitem.fpartno = inmastx.fpartno AND rcitem.fpartrev = inmastx.frev " +
                    " WHERE (rcitem.freceiver = '" + TextBox_Reciver.Text + "')");
        }
   }
}

推荐答案





我可以看到你正在创建BindingSource但是你没有将BindingSource分配给dataGridView。您必须使用以下行将数据源绑定到网格。



Hi,

I can see you are creating the BindingSource but you are not assigning the BindingSource to the dataGridView. You have to use the following line to bind the datasource to the grid.

dataGridView2.DataSource = bindingSource1;





添加此行,您的网格将与您从数据库中填充的数据绑定。



谢谢

Sisir Patro



Add this line and your grid will be binded with the data that you are populating from database.

Thanks
Sisir Patro


By observing this working code I've found that everything of your code is correct except that in the form load event or any other time you should set the property AutoGenerateColumns of your dataGridView to "true"




dataGridView2.AutoGenerateColumns = true;







The Complete code is given below observe and fix yours







public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn("EmployeeID", typeof(int));
            DataColumn dc1 = new DataColumn("EmployeeName", typeof(string));
            dt.Columns.Add(dc); dt.Columns.Add(dc1);
            DataRow row = dt.NewRow();

            row["EmployeeID"] = 100;
            row["EmployeeName"] = "ANIS";

            dt.Rows.Add(row);

            row = dt.NewRow();
            row["EmployeeID"] = 101;
            row["EmployeeName"] = "SUMON";

            dt.Rows.Add(row);

            bindingSource1.DataSource = dt;

            DataGridViewButtonColumn buttonColumn =
                new DataGridViewButtonColumn();
            buttonColumn.HeaderText = "";
            buttonColumn.Name = "Status Request";
            buttonColumn.Text = "Select";
            buttonColumn.UseColumnTextForButtonValue = true;
            buttonColumn.DefaultCellStyle.BackColor = Color.DodgerBlue;
            buttonColumn.DefaultCellStyle.SelectionBackColor = Color.Yellow;

            dataGridView1.Columns.Add(buttonColumn);
            


            
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            dataGridView1.AutoGenerateColumns = true;
        }
    }


这篇关于dataGridView - 网格不拉入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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