You must Sign In to post a response.
  • Category: Windows

    How to Binding data to datagridview

    Are you looking for a way to Binding data to datagridview ? then read this thread to know how to bind it

    Hi All,

    I am trying to bind the data into Datagridview.
    I have written the below code for Binding the data to DataGridview, but I cant see the SQL output in DataGridview Control.

    SqlDataAdapter sda = new SqlDataAdapter(loSql, scon)
    DataSet ds = new DataSet()
    sda.Fill(ds, "Fase2")
    bindingSource1.DataSource = ds.Tables["Fase2"]
    dataGridView1.DataSource = bindingSource1

    Can you please help here to display the content in dataGridView1?


    Manoj R
  • #723352
    Go through the following code snippet it will help you more to accomplish your task
    no need to use binding source you can directly attach code

    Sqlcommand objCMD= new Sqlcommand("Select * from EMP");

    sqlDataAdapter1 = new SqlDataAdapter();

    sqlDataAdapter1.SelectCommand = objCMD;

    DataSet ds = new DataSet();

    //fill dataset with adapter

    //bind dataset table to gridview directly
    datagridview1.DataSource = ds.table(0);

    hope it helps

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #723359
    Refer below code sample to bind data in datagridview

    sing System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    namespace cshwin

    public partial class Form1 : Form
    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();

    public Form1()
    private void Form1_Load(object sender, EventArgs e)
    sqlcmd = new SqlCommand("select * from emp", sqlcon);
    da = new SqlDataAdapter(sqlcmd);

    Your Hard work never fails

  • #723360
    The DataGridView control provides facilities of standard Windows Forms for data binding model, You can you this given guideline code for Binding data to datagridview
    private void Form2_Load(object sender, System.EventArgs e)

    dataGridView2.DataSource = bindingSource2;
    GetData("select * from Example");

  • #723363

    Use below lines of code this will help you..

    Declare global varriables
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;

    And in formload event wrote below lines of code

    protected void Form1_Load(object sender, EventArgs e)
    con = new SqlConnection("DataBase=naveen;User id=sa;Password=P@ssword9");
    cmd=new SqlCommand("select * from emp",con);

    da=new SqlDataAdapter(cmd);
    DataGridView1.DataSource = ds; con.Close();

    Hope this information is helpful to you....

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #723390
    Here is a sample code to Bind a grid. You can make use of a data set for doing so.

    SqlConnection con = new SqlConnection(ConnStr);
    string strQuery = "select Name from emp where Id>100";

    SqlDataAdapter ada = new SqlDataAdapter(strQuery, con);
    DataSet lds = new DataSet();
    gvSales.DataSource = lds;

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #726352
    Hi Manoj Raut,

    You can follow the example using C# what I describe below:
    SqlConnection cnEmp = new SqlConnection(ConnectionString);
    string qryEmp = "select ID, Name from Employee;
    SqlDataAdapter sdaEmp = new SqlDataAdapter(qryEmp, cnEmp);
    DataSet dsEmp = new DataSet();
    GridView.DataSource = dsEmp;

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
    Employee emp = e.Row.DataItem as Employee;
    e.Row.Cells[0].Text =Convert.ToString(emp.ID);
    e.Row.Cells[1].Text =(emp.Name);

    Best regards

  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.