![]() Christian Moser's WPF Tutorial. An elaborate tutorial about the Windows Presentation Foundation with hunderts of samples. Here Mudassar Ahmed Khan has explained with an example, how to bind / fill / populate DataGridView control with DataSet in Windows Forms (WinForms) Application in C#. Bind / Fill / Populate Data. Grid. View with Data. Set in Windows Forms (Win. Forms) Application in C# and VB. Thanks for choosing DevExpress for your software development needs. We are your extended team and are working hard to make certain you have all the resources.Net. In this article I will explain with an example, how to bind / fill / populate Data. Grid. View control with Data. Set in Windows Forms (Win. Forms) Application in C# and VB. Net. Database. For this article I am making use of the Microsoft’s Northwind Database. Download and install instructions are provided in the link below. Adding a Data. Grid. View to the Windows Form Firstly you need to add a Data. Grid. View control to the Windows Form from the Visual Studio Tool. Box as shown below. Namespaces. You will need to import the following namespace. C#using System. Data; using System. Data. Sql. Client; VB. Net. Imports System. Data. Imports System. Data. Sql. Client. Simple binding of data to Data. Grid. View from Database using. Data. Set. Below is the simple and straight forward way of binding data to Data. Grid. View control using. Data. Set. In the below code, the Data. Set is populated from Customers Table of Northwind Database and then it is set as Data. Source to the Data. Grid. View control. In this approach all columns returned by the Select Query will be displayed in the Data. Grid. View. C#publicpartialclass. Form. 1 : Form{ public Form. Initialize. Component(); Bind. Grid(); } privatevoid Bind. Grid() { string constring = @"Data Source=.\SQL2. Initial Catalog=Northwind; User id = sa; password=pass@1. Sql. Connection con = new. Sql. Connection(constring)) { using (Sql. Command cmd = new. Sql. Command("SELECT * FROM Customers", con)) { cmd. Command. Type = Command. Type. Text; using (Sql. Data. Adapter sda = new. Sql. Data. Adapter(cmd)) { using (Data. Set ds = new. Data. Set()) { sda. Fill(ds); data. Grid. View. 1. Data. Source = ds. Tables[0]; } } } } }}VB. Net. Public. Class. Form. 1 Public. Sub. New() Initialize. Component() Bind. Grid() End. Sub Private. Sub Bind. Grid() Dim constring As. String = "Data Source=.\SQL2. Initial Catalog=Northwind; User id = sa; password=pass@1. Using con As. New. Sql. Connection(constring) Using cmd As. New. Sql. Command("SELECT * FROM Customers", con) cmd. Command. Type = Command. Type. Text Using sda As. New. Sql. Data. Adapter(cmd) Using. As. New. Data. Set() sda. Fill(ds) data. Grid. View. 1. Data. Source = ds. Tables(0) End. Using End. Using End. Using End. Using End. Sub. End. Class. Populate Specific (Certain) columns to Data. Grid. View control using Data. Set. Sometimes requirement is such that you would like to bind only some specific (certain) columns and would not like to show all columns as done in the above approach. For such cases we need to Auto. Generate. Columns property to False (default True) and add the columns you want to show in Data. Grid. View using code. While adding columns it is necessary to set the following properties. Name: Unique Name of the Data. Grid. View Column. Header. Text: Header Text of the Data. Grid. View Column. Data. Property. Name: Name of the Data Column Field that will be displayed in the Data. Grid. View Column. C#publicpartialclass. Form. 1 : Form{ public Form. Initialize. Component(); Bind. Grid(); } privatevoid Bind. Grid() { string constring = @"Data Source=.\SQL2. Initial Catalog=Northwind; User id = sa; password=pass@1. Sql. Connection con = new. Sql. Connection(constring)) { using (Sql. Command cmd = new. Sql. Command("SELECT * FROM Customers", con)) { cmd. Command. Type = Command. Type. Text; using (Sql. Data. Adapter sda = new. Sql. Data. Adapter(cmd)) { using (Data. Set ds = new. Data. Set()) { sda. Fill(ds); //Set Auto. Generate. Columns False data. Grid. View. 1. Auto. Generate. Columns = false; //Set Columns Count data. Grid. View. 1. Column. Count = 3; //Add Columns data. Grid. View. 1. Columns[0]. Name = "Customer. Id"; data. Grid. View. 1. Columns[0]. Header. Text = "Customer Id"; data. Grid. View. 1. Columns[0]. Data. Property. Name = "Customer. ID"; data. Grid. View. 1. Columns[1]. Header. Text = "Contact Name"; data. Grid. View. 1. Columns[1]. Name = "Name"; data. Grid. View. 1. Columns[1]. Data. Property. Name = "Contact. Name"; data. Grid. View. 1. Columns[2]. Name = "Country"; data. Grid. View. 1. Columns[2]. Header. Text = "Country"; data. Grid. View. 1. Columns[2]. Data. Property. Name = "Country"; data. Grid. View. 1. Data. Source = ds. Tables[0]; } } } } }}VB. Net. Public. Class. Form. 1 Public. Sub. New() Initialize. Component() Bind. Grid() End. Sub Private. Sub Bind. Grid() Dim constring As. String = "Data Source=.\SQL2. Initial Catalog=Northwind; User id = sa; password=pass@1. Using con As. New. Sql. Connection(constring) Using cmd As. New. Sql. Command("SELECT * FROM Customers", con) cmd. Command. Type = Command. Type. Text Using sda As. New. Sql. Data. Adapter(cmd) Using. As. New. Data. Set() sda. Fill(ds) 'Set Auto. Generate. Columns False data. Grid. View. 1. Auto. Generate. Columns = False 'Set Columns Count data. Grid. View. 1. Column. Count = 3 'Add Columns data. Grid. View. 1. Columns(0). Name = "Customer. Id" data. Grid. View. 1. Columns(0). Header. Text = "Customer Id" data. Grid. View. 1. Columns(0). Data. Property. Name = "Customer. ID" data. Grid. View. 1. Columns(1). Name = "Name" data. Grid. View. 1. Columns(1). Header. Text = "Contact Name" data. Grid. View. 1. Columns(1). Data. Property. Name = "Contact. Name" data. Grid. View. 1. Columns(2). Name = "Country" data. Grid. View. 1. Columns(2). Header. Text = "Country" data. Grid. View. 1. Columns(2). Data. Property. Name = "Country" data. Grid. View. 1. Data. Source = ds. Tables(0) End. Using End. Using End. Using End. Using End. Sub. End. Class. Downloads.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |