How to bind a list with a dataGridView in windows forms?
Here i will show you to how to bind a list with a dataGridView Object in windows forms, i will be using a simple string list , this list will have a few strings in it which will be displayed in the dataGridView
Create a windows forms application with the name "wfaBindingGridView" , add the DataGridView tool from the toolbox into your form as shown below. and add a button into the form .
Double click on the Bind Grid button and you will be taken to the the codebehind c# file of this form with the event that handles this button click.
The code will be as below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace wfaBindingGridView
{
public partial class Form1 : Form
{
List<String> strList = new List<string>();
public Form1()
{
InitializeComponent();
}
void BindGrid()
{
strList.Add("Str 1");
strList.Add("Str 2");
strList.Add("Str 3");
strList.Add("Str 4");
strList.Add("Str 5");
strList.Add("Str 6");
DataTable dt = new DataTable();
dt.Columns.Add("Strings");
foreach (String s in strList)
{
DataRow dr = dt.NewRow();
dr["Strings"] = s;
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
BindGrid();
}
}
}
Click on the Bind Grid button and you will see the below data coming into the grid.
You can tweak the same program to work with lists of any type of objects.
Thanks for visiting this Post, please comment if this needs any more information for easy understanding.
Here i will show you to how to bind a list with a dataGridView Object in windows forms, i will be using a simple string list , this list will have a few strings in it which will be displayed in the dataGridView
Create a windows forms application with the name "wfaBindingGridView" , add the DataGridView tool from the toolbox into your form as shown below. and add a button into the form .
Double click on the Bind Grid button and you will be taken to the the codebehind c# file of this form with the event that handles this button click.
- Create a method called BindGrid as below
- Declare a List of strings as below , then add some data into it .
- The method will be called inside the click event of the button.
The code will be as below
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace wfaBindingGridView
{
public partial class Form1 : Form
{
List<String> strList = new List<string>();
public Form1()
{
InitializeComponent();
}
void BindGrid()
{
strList.Add("Str 1");
strList.Add("Str 2");
strList.Add("Str 3");
strList.Add("Str 4");
strList.Add("Str 5");
strList.Add("Str 6");
DataTable dt = new DataTable();
dt.Columns.Add("Strings");
foreach (String s in strList)
{
DataRow dr = dt.NewRow();
dr["Strings"] = s;
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
BindGrid();
}
}
}
The output:
You can tweak the same program to work with lists of any type of objects.
Thanks for visiting this Post, please comment if this needs any more information for easy understanding.
No comments:
Post a Comment