在我的表单中,我有一个DataGridView。以及用于输入和输出数据的控件。在网格视图中显示数据时。我有两次打电话给BindGrid。一个是formload,另一个是在添加新记录之后。

 public formAccounts()
    {
        InitializeComponent();
        BindGrid();
    }

 private void BindGrid()
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= G:\Sanjeev\TESTDB\DB.mdb;Jet OLEDB:Database Password=Test123; Jet OLEDB:Engine Type=5";
        conn.Open();
        string constring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\Sanjeev\TESTDB\DB.mdb;Jet OLEDB:Database Password=Test123; Jet OLEDB:Engine Type=5";

        using (OleDbConnection con = new OleDbConnection(constring))
        {
            using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM tblAccounts", con))
            {
                cmd.CommandType = CommandType.Text;
                using (OleDbDataAdapter sda = new OleDbDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        //Set AutoGenerateColumns False
                        dtGrdAccounts.AutoGenerateColumns = false;
                        //Set Columns Count
                        ***dtGrdAccounts.ColumnCount = 3;***
                        //Add Columns
                        dtGrdAccounts.Columns[0].Name = "AccountID";
                        dtGrdAccounts.Columns[0].HeaderText = "Id";
                        dtGrdAccounts.Columns[0].DataPropertyName = "AccID";

                        dtGrdAccounts.Columns[1].HeaderText = "Account name";
                        dtGrdAccounts.Columns[1].Name = "Account name";
                        dtGrdAccounts.Columns[1].DataPropertyName = "AccName";

                        dtGrdAccounts.Columns[2].Name = "AccountNumber";
                        dtGrdAccounts.Columns[2].HeaderText = "Account number";
                        dtGrdAccounts.Columns[2].DataPropertyName = "AccNumber";
                        dtGrdAccounts.DataSource = dt;
                    }
                }
            }
        }
    }


如果删除行dtGrdAccounts.ColumnCount = 3;,我将得到以下错误。
    Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

添加新记录后,在“保存”按钮上,我调用BindGrid()方法以在GridiView中获取记录。

string cmdText = "prAddAccounts";
OleDbCommand cmd = new OleDbCommand(cmdText, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("AccName", txtAccName.Text.ToString());
 //..... other parameters  to insert record .....
OleDbCommand cmd = new OleDbCommand(cmdText, conn);
cmd.CommandType = CommandType.StoredProcedure;
OleDbDataReader reader = cmd.ExecuteReader();
BindGrid();

最佳答案

问题是当您再次调用BindGrid()时,必须先将其清除:

string cmdText = "prAddAccounts";
OleDbCommand cmd = new OleDbCommand(cmdText, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("AccName", txtAccName.Text.ToString());
 //..... other parameters  to insert record .....

//Clear the binding.
dtGrdAccounts.DataSource = null;
//Bind the new data.
BindGrid();

07-27 13:59