Question:

How can i populate a data grid with Info from a query in C# using ADO?

by  |  earlier

0 LIKES UnLike

Anyone? I have three (3) tables. I need to populate a data grid in C# with info resulting from a Query based on these 3 tables.

I will gladly appreciate any and all help.

 Tags:

   Report

1 ANSWERS


  1. Here is an example of how to retrieve data from the database and populating the data set with it. You can change the connection string and the select query to fit you needs.

    ASP.NET

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataGrid.aspx.cs" Inherits="WebApplication1.DataGrid" %>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

        <title>Untitled Page</title>

    </head>

    <body>

        <form id="form1" runat="server">

        <div>

    <asp:GridView ID="GridView1" runat="server">

    </asp:GridView>

        </div>

        </form>

    </body>

    </html>

    C# code

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Data.SqlClient;

    namespace WebApplication1

    {

    public partial class DataGrid : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    DataSet _ds = new DataSet();

    SqlConnection _conn = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True");

    SqlCommand _command = new SqlCommand();

    _command.Connection = _conn;

    _command.CommandText = "select * from dbo.Employees";

    _command.CommandType = CommandType.Text;

    SqlDataAdapter _da = new SqlDataAdapter(_command);

    _da.Fill(_ds);

    GridView1.DataSource = _ds;

    GridView1.DataBind();

    }

    }

    }

    Hope this helps.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.