How to bind data with data table in gridview in asp.net c#
Source code:-
    <asp:GridView ID="GridView1"
runat="server"
BackColor="White"
        BorderColor="#999999"
BorderStyle="None"
BorderWidth="1px"
CellPadding="3"
        GridLines="Vertical">
        <FooterStyle BackColor="#CCCCCC"
ForeColor="Black"
/>
        <HeaderStyle BackColor="#000084"
Font-Bold="True"
ForeColor="White"
/>
        <PagerStyle BackColor="#999999"
ForeColor="Black"
HorizontalAlign="Center"
/>
        <RowStyle BackColor="#EEEEEE"
ForeColor="Black"
/>
        <SelectedRowStyle
BackColor="#008A8C"
Font-Bold="True"
ForeColor="White"
/>
        <SortedAscendingCellStyle
BackColor="#F1F1F1"
/>
        <SortedAscendingHeaderStyle
BackColor="#0000A9"
/>
        <SortedDescendingCellStyle
BackColor="#CAC9C9"
/>
        <SortedDescendingHeaderStyle
BackColor="#000065"
/>
    </asp:GridView>
Design:-
C# code:-
protected void Page_Load(object sender, EventArgs
e)
    {
        if(!IsPostBack)
         {
         
BindGridviewData();
         }
    }
    protected void
BindGridviewData()
    {
        DataTable dt = new
DataTable();
       
dt.Columns.Add("Id", typeof(Int32));
       
dt.Columns.Add("Name", typeof(string));
       
dt.Columns.Add("Male", typeof(string));
       
dt.Columns.Add("Location", typeof(string));
        DataRow dtrow = dt.NewRow();    // Create New Row
       
dtrow["Id"] = 1;            //Bind
Data to Columns
       
dtrow["Name"] = "Neeraj Srivastava";
       
dtrow["Male"] = "Male";
       
dtrow["Location"] = "Hyderabad";
       
dt.Rows.Add(dtrow);
        dtrow
= dt.NewRow();               // Create New Row
       
dtrow["Id"] = 2;               //Bind
Data to Columns
       
dtrow["Name"] = "Dheeraj Srivastava";
       
dtrow["Male"] = "Male";
       
dtrow["Location"] = "Lucknow";
       
dt.Rows.Add(dtrow);
        dtrow
= dt.NewRow();              // Create New Row
       
dtrow["Id"] = 3;              //Bind
Data to Columns
       
dtrow["Name"] = "Lokdendra Bundela";
       
dtrow["Male"] = "Male";
       
dtrow["Location"] = "Luckbow";
       
dt.Rows.Add(dtrow);
       
GridView1.DataSource = dt;
       
GridView1.DataBind();
    }
}
How to bind data with data table in gridview in asp.net c#
 
        Reviewed by NEERAJ SRIVASTAVA
        on 
        
4:38:00 PM
 
        Rating: 
 
        Reviewed by NEERAJ SRIVASTAVA
        on 
        
4:38:00 PM
 
        Rating: 

No comments: