Saturday, October 4, 2014

Databinding a DataTable of String Values to a Windows DataGrid Control

10:45 AM - By azeheruddin khan 0

This example creates a data table with two columns and two rows and then binds it to an existing DataGrid control.


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 StringDataGrid
{
    public partial class HomeForm : Form
    {
        public HomeForm()
        {
            InitializeComponent();
        }

        private void HomeForm_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = GetDataTable();
        }

        private DataTable GetDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Item", typeof(string)));
            dt.Columns.Add(new DataColumn("Color", typeof(string)));
            dt.Rows.Add(new string[] { "TV", "Black" });
            dt.Rows.Add(new string[] { "Mobile", "White" });
            dt.Rows.Add(new string[] { "Chair", "Red" });          
            return dt;
        }
    }
}




About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top