how to bind datatable to datagridview in c#

前端 未结 7 644
面向向阳花
面向向阳花 2020-11-27 20:50

I need to bind my DataTable to my DataGridView. i do this:

        DTable = new DataTable();
        SBind = new BindingSou         


        
相关标签:
7条回答
  • 2020-11-27 21:23

    Even better:

    DataTable DTable = new DataTable();
    BindingSource SBind = new BindingSource();
    SBind.DataSource = DTable;
    DataGridView ServersTable = new DataGridView();
    
    ServersTable.AutoGenerateColumns = false;
    ServersTable.DataSource = DTable;
    
    ServersTable.DataSource = SBind;
    ServersTable.Refresh();
    

    You're telling the bindable source that it's bound to the DataTable, in-turn you need to tell your DataGridView not to auto-generate columns, so it will only pull the data in for the columns you've manually input into the control... lastly refresh the control to update the databind.

    0 讨论(0)
  • 2020-11-27 21:24

    for example we want to set a DataTable 'Users' to DataGridView by followig 2 steps : step 1 - get all Users by :

    public DataTable  getAllUsers()
        {
            OracleConnection Connection = new OracleConnection(stringConnection);
            Connection.ConnectionString = stringConnection;
            Connection.Open();
    
            DataSet dataSet = new DataSet();
    
            OracleCommand cmd = new OracleCommand("semect * from Users");
            cmd.CommandType = CommandType.Text;
            cmd.Connection = Connection;
    
            using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
            {
                dataAdapter.SelectCommand = cmd;
                dataAdapter.Fill(dataSet);
            }
    
            return dataSet.Tables[0];
        }
    

    step 2- set the return result to DataGridView :

    public void setTableToDgv(DataGridView DGV, DataTable table)
        {
            DGV.DataSource = table;
        }
    

    using example:

        setTableToDgv(dgv_client,getAllUsers());
    
    0 讨论(0)
  • 2020-11-27 21:30

    // I built my datatable first, and populated it, columns, rows and all. //Then, once the datatable is functional, do the following to bind it to the DGV. NOTE: the DGV's AutoGenerateColumns property must be 'true' for this example, or the "assigning" of column names from datatable to dgv will not work. I also "added" my datatable to a dataset previously, but I don't think that is necessary.

     BindingSource SBind = new BindingSource();
     SBind.DataSource = dtSourceData;
    
     ADGView1.AutoGenerateColumns = true;  //must be "true" here
     ADGView1.Columns.Clear();
     ADGView1.DataSource = SBind;
    
     //set DGV's column names and headings from the Datatable properties
     for (int i = 0; i < ADGView1.Columns.Count; i++)
     {
           ADGView1.Columns[i].DataPropertyName = dtSourceData.Columns[i].ColumnName;
           ADGView1.Columns[i].HeaderText = dtSourceData.Columns[i].Caption;
     }
     ADGView1.Enabled = true;
     ADGView1.Refresh();
    
    0 讨论(0)
  • 2020-11-27 21:32
    private void Form1_Load(object sender, EventArgs e)
        {
            DataTable StudentDataTable = new DataTable("Student");
    
            //perform this on the Load Event of the form
            private void AddColumns() 
            {
                StudentDataTable.Columns.Add("First_Int_Column", typeof(int));
                StudentDataTable.Columns.Add("Second_String_Column", typeof(String));
    
                this.dataGridViewDisplay.DataSource = StudentDataTable;
            }
        }
    
        //Save_Button_Event to save the form field to the table which is then bind to the TableGridView
        private void SaveForm()
            {
                StudentDataTable.Rows.Add(new object[] { textBoxFirst.Text, textBoxSecond.Text});
    
                dataGridViewDisplay.DataSource = StudentDataTable;
            }
    
    0 讨论(0)
  • 2020-11-27 21:34

    On the DataGridView, set the DataPropertyName of the columns to your column names of your DataTable.

    0 讨论(0)
  • 2020-11-27 21:35
    foreach (DictionaryEntry entry in Hashtable)
    {
        datagridviewTZ.Rows.Add(entry.Key.ToString(), entry.Value.ToString());
    }
    
    0 讨论(0)
提交回复
热议问题