Bind C# GridView to DataTable or DataSet
The Kettic GridView C#.NET component allows users of the GridView control easily binding to database table or dataset. This way of data binding is really easy and requires no complex process. The data binding operation only need the data objects implement one of these interfaces, IList, IEnumarable, or ICollection. Additionally, the powerful data grid control supports out of the box binding to objects in sub directory as well as their properties for customizing.
Bind GridView to Data Table or DataSet in C# project
The Kettic GridView component allows users of the GridView control easily binding to database table or dataset. The following is a guide that shows how to bind the GridView control to a single data table. When we want to binding the GridView to multiple data tables, we can achieve that by referring to hierarchical data binding.
- Drag a KetticGridView component from the toolbox and drop on a form in your C#.NET project.
- Change the value of the Dock property as Fill to dock the Window to GridView
- Open the Properties window of the GridView component on the Form, go to the DataSource property, and open the list by clicking the arrow.
- Choose the Add Project Data Source... link to show the Data Source Configuration Wizard. And then select a Data Source Type page in the Wizard, click the Database icon, and hit the Next button.
- Choose the New Connection... button in the Choose Your data Connection page to show the Add Connection dialog. And then hit the Change... button to show the Change Data Source dialog.
- In the Change Data Source dialog, choose the Microsoft Access Database File data source. After finishing it, hit the OK button to close the dialog.
- When the Add Connection dialog display, we can choose the Database File Name Browse button and find the database table, NWind.mdb, included in the GridView control, which is under the path: \KetticGridViewWinForms\DataSamples. Close the dialog by hitting the OK button.
- Choose the Categories checkbox in the Choose Your Database Objects page and close the Data Source Configuration Wizard by hitting the Finish button.
- Choose the Categories data table from the grid DataSource property in the Properties window. Then the DataSet, BindingSource and TableAdapter objects will be generated for the categories table.
- Now we can copy the C# code below, which will iterate all the columns in the grid and calls BestFit() to expand columns for displaying data, and paste them to the Form_Load event handler. After that, debug the C#.NET Windows Forms project to see the result.
private void GuideBindingToDataTableOrDataSet_Load(object sender, EventArgs e)
foreach (GridViewDataColumn column in ketticGridView1.Columns)