Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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"}


C#
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 + "')");
        }
   }
}
Posted
Updated 31-Dec-14 11:42am
v2

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;


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

Thanks
Sisir Patro
 
Share this answer
 
Comments
Daniel 1102 2-Jan-15 19:22pm    
Adding the line of code you suggested fixed the issue I was having with the grid not showing up. Thanks for your help.
[no name] 6-Jan-15 6:58am    
Great... Enhoy coding bro...
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"

C#
dataGridView2.AutoGenerateColumns = true;


The Complete code is given below observe and fix yours


C#
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;
        }
    }
 
Share this answer
 
Comments
Daniel 1102 2-Jan-15 19:21pm    
Thank you for taking the time to help me trouble shoot the problem, but adding the “dataGridView1.AutoGenerateColumns = true;” to the Form1_Load did not resolve it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900