无法在数据绑定datagridview控件c#上设置列计数属性

时间:2014-04-07 05:29:03

标签: c# winforms data-binding datagridview

在我的表单中,我有一个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

添加新记录后,On按钮保存我调用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();

2 个答案:

答案 0 :(得分:3)

问题是,当您再次致电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();

答案 1 :(得分:0)

如果您多次调用 datagridview 列设计代码,那么 在设置列属性之前,您必须将 Gridview 数据源设置为 null 如下图

<块引用>

dataGridView1.DataSource = null;

import pymongo
from pymongo import MongoClient

cluster = MongoClient("mongodb+srv://xxxx:xxxx@xxxxx/<>?retryWrites=true&w=majority")
db = cluster["test"]
collection = db["test"]

post = {"_id": "0", "name": "tim", "score": 5}
while True:
    try:
        collection.insert_one(post)
        break
    except:
        collection.update_one({"_id":"0"},{"$set": {"name":"tim", "score":5 }})
        break