Bind (Populate) GridView using C# in ASP.Net

Last Reply 28 days ago By indradeo

Posted 28 days ago

Dear Sir,

can any one suggest me how to display database selected value in gridvew using asp c#.

Regard,

indradeo

.aspx

<%@ Page Language="C#" AutoEventWireup="true" Codefile="teldir.aspx.cs" Inherits="Url.teldir" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>  <form id="form1" runat="server">
  
    <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="402px">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>     
    </div>
    </form>
</body>
</html>

.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Url
{
    public partial class teldir : System.Web.UI.Page
    {       
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["myconnection"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM emp_mast1 WHERE dept_code ='08'";
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
        }
    }
}

 

Posted 28 days ago

Dear Team,

Thanks for help.

.aspx

<%@ Page Language="C#" AutoEventWireup="true" Codefile="teldir.aspx.cs" Inherits="Url.teldir" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h1>Department Directory</h1>
    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>
</body>
</html>

 .aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace Url
{
    public partial class teldir : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();


            con.ConnectionString = ConfigurationManager.ConnectionStrings["myconnection"].ToString();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            string s = "select * from emp_mast1 where dept_code=08";
            SqlDataAdapter da = new SqlDataAdapter(s, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            //to show dATA
            GridView1.DataBind();
            con.Close();
        }
    }
}

Thanks to asp team.

Regards,

Indradeo