【问题标题】:Connect BindingNavigator with Programmatically Created Datagridview将 BindingNavigator 与以编程方式创建的 Datagridview 连接
【发布时间】:2012-12-11 07:25:12
【问题描述】:

我以编程方式创建了一个数据网格视图。 所以没有绑定源或数据源可以将 Datagridview 和 Binidgnavigator 连接到它们。 有没有其他方法可以将它们相互连接。 这是我的 datafridview 代码

帮我把它连接到 bindingNavigator

private void Fill()
{
    try
    {
        if (dataGridView1 != null)
        {
            dataGridView1.ColumnCount = 11;
            dataGridView1.Columns[0].HeaderText = Resources.Form1_Fill_ID;
            dataGridView1.Columns[1].HeaderText = Resources.Form1_Fill_Family;
            dataGridView1.Columns[2].HeaderText = Resources.Form1_Fill_Cellphone;
            dataGridView1.Columns[3].HeaderText = Resources.Form1_Fill_Phone1;
            dataGridView1.Columns[4].HeaderText = Resources.Form1_Fill_Phone2;
            dataGridView1.Columns[5].HeaderText = Resources.Form1_Fill_Phone3;
            dataGridView1.Columns[6].HeaderText = Resources.Form1_Fill_Fax;
            dataGridView1.Columns[7].HeaderText = Resources.Form1_Fill_CompanyName;
            dataGridView1.Columns[8].HeaderText = Resources.Form1_Fill_Agency;
            dataGridView1.Columns[9].HeaderText = Resources.Form1_Fill_Brands;
            dataGridView1.Columns[10].HeaderText = Resources.Form1_Fill_Address;

            dataGridView1.Columns[0].Name = Resources.Form1_Fill_ID;
            dataGridView1.Columns[1].Name = Resources.Form1_Fill_Family;
            dataGridView1.Columns[2].Name = Resources.Form1_Fill_Cellphone;
            dataGridView1.Columns[3].Name = Resources.Form1_Fill_Phone1;
            dataGridView1.Columns[4].Name = Resources.Form1_Fill_Phone2;
            dataGridView1.Columns[5].Name = Resources.Form1_Fill_Phone3;
            dataGridView1.Columns[6].Name = Resources.Form1_Fill_Fax;
            dataGridView1.Columns[7].Name = Resources.Form1_Fill_CompanyName;
            dataGridView1.Columns[8].Name = Resources.Form1_Fill_Agency;
            dataGridView1.Columns[9].Name = Resources.Form1_Fill_Brands;
            dataGridView1.Columns[10].Name = Resources.Form1_Fill_Address;
        }

        _conn.ConnectionString = _connectionString;
        var cmd = new OleDbCommand("Select * from contacts ", _conn);
        _conn.Open();
        OleDbDataReader reader = cmd.ExecuteReader();

        int i = 0;
        while (reader != null && reader.Read())
        {
            if (dataGridView1 != null)
            {
                dataGridView1.Rows.Add(1);
            }
            if (dataGridView1 != null)
            {
                var row = dataGridView1.Rows[i];

                row.Cells[Resources.Form1_Fill_ID].Value = reader[0].ToString();
                row.Cells[Resources.Form1_Fill_Family].Value = reader[1].ToString();
                row.Cells[Resources.Form1_Fill_Cellphone].Value = reader[2].ToString();
                row.Cells[Resources.Form1_Fill_Phone1].Value = reader[3].ToString();
                row.Cells[Resources.Form1_Fill_Phone2].Value = reader[4].ToString();
                row.Cells[Resources.Form1_Fill_Phone3].Value = reader[5].ToString();
                row.Cells[Resources.Form1_Fill_Fax].Value = reader[6].ToString();
                row.Cells[Resources.Form1_Fill_CompanyName].Value = reader[7].ToString();
                row.Cells[Resources.Form1_Fill_Agency].Value = reader[8].ToString();
                row.Cells[Resources.Form1_Fill_Brands].Value = reader[9].ToString();
                row.Cells[Resources.Form1_Fill_Address].Value = reader[10].ToString();

            }
            i++;
        }

    }
    catch (Exception ex)
    {
        return;
    }
    finally
    {
        _conn.Close();
    }
}

【问题讨论】:

    标签: c# binding datagridview


    【解决方案1】:

    尝试修改您的代码。您不需要以编程方式创建列,您的 查询 本身可以通过 DataTableBindingSource 创建一个列,试试这个代码并获得一些想法。

    BindingNavigator 和 BindingSource

            string connectionString =
                @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                @"Data Source=D:\myDatabase.accdb;";
    
            string queryString = "SELECT Name AS FullName, Gender AS Gender, Address AS [Current Address] FROM Person";
    
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            using (OleDbCommand command = new OleDbCommand(queryString, connection))
            {
                try
                {
                    BindingSource bs = new BindingSource();
                    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(queryString, connection);
                    OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
                    DataTable dataTable = new DataTable();
                    dataAdapter.Fill(dataTable);
                    bs.DataSource = dataTable;
                    dataGridView1.DataSource = bs;
                    bindingNavigator1.BindingSource = bs;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多