how to add columns having templatefield dynamically in gridview in asp.net
how to add columns having templatefield dynamically in gridview in asp.net using c#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Dhanashri PatilPosted Sep 12, 2012, 9:25 AM
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<title>Untitled Pagetitle>
head>
<
body>
<form id="form1" runat="server">
<div>
<%
--
%>
<asp:Label ID="Label1" runat="server" Text="Choose Class" Font-Bold="True">asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
Width="162px">
asp:DropDownList><br />
<br />
<br />
<asp:Label runat="server" ID="ColumnsAsRows" Text="Columns As Rows" />
<asp:GridView runat="server" ID="gvColumnsAsRows" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"
GridLines="None" CellSpacing="5" Width="323px" >
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="10px" />
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
asp:GridView>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
CellPadding="2" DataSourceID="SqlDataSource1" ForeColor="Black"
GridLines="None" CellSpacing="10" >
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="15px" />
<Columns>
<asp:TemplateField HeaderText="weekdays" SortExpression="weekdays">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("weekdays") %>'>asp:Label>
ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("weekdays") %>'>asp:TextBox>
EditItemTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Subjects">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="SubjectName"
DataValueField="ClassId">
<asp:ListItem>Englishasp:ListItem>
<asp:ListItem>Mathematicsasp:ListItem>
<asp:ListItem>Historyasp:ListItem>
<asp:ListItem>Algebraasp:ListItem>
<asp:ListItem>scienceasp:ListItem>
<asp:ListItem>Geographyasp:ListItem>
asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwindConnectionString %>"
SelectCommand="SELECT [SubjectName], [ClassId] FROM [Subjects] WHERE ([ClassId] = @ClassId)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ClassId"
PropertyName="SelectedValue" Type="Int32" />
SelectParameters>
asp:SqlDataSource>
ItemTemplate>
asp:TemplateField>
Columns>
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwindConnectionString %>"
SelectCommand="SELECT [weekdays] FROM [weekdays] WHERE ([ClassId] = @ClassId)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="ClassId"
PropertyName="SelectedValue" Type="Int32" />
SelectParameters>
asp:SqlDataSource>
<br />
<br />
div>
form>
body>
html>
using
System;
using
System.Configuration;
using
System.Data;
using
System.Data.SqlClient;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public
partial class _Default : System.Web.UI.Page
{
SqlDataAdapter dadapter;
DataSet dset;
string connstring = "Data Source=SMART-PC;Initial Catalog=MyNorthwind;Integrated Security=True";
string sql = "select * from Class";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dadapter =
new SqlDataAdapter(sql, connstring);
dset =
new DataSet();
dadapter.Fill(dset);
DropDownList1.DataSource = dset.Tables[0];
DropDownList1.DataTextField =
"ClassName";
DropDownList1.DataValueField =
"ClassId";
DropDownList1.DataBind();
}
SqlConnection con = new SqlConnection("Data Source=SMART-PC;Initial Catalog=MyNorthwind;Integrated Security=True");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select Starttime,Endtime from TimeTableMain where ClassId='" + DropDownList1.SelectedValue + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
gvColumnsAsRows.DataSource = FlipDataTable(dt);
gvColumnsAsRows.DataBind();
gvColumnsAsRows.HeaderRow.Visible =
false;
con.Close();
}
public static DataTable FlipDataTable(DataTable dt)
{
DataTable table = new DataTable();
//Get all the rows and change into columns
for (int i = 0; i <= dt.Rows.Count; i++)
{
table.Columns.Add(
Convert.ToString(i));
}
DataRow dr;
//get all the columns and make it as rows
for (int j = 0; j < dt.Columns.Count; j++)
{
dr = table.NewRow();
dr[0] = dt.Columns[j].ToString();
for (int k = 1; k <= dt.Rows.Count; k++)
dr[k] = dt.Rows[k - 1][j];
table.Rows.Add(dr);
}
return table;
}
}
Rewant ChoudharyPosted Sep 12, 2012, 8:23 AM
Dhanashri PatilPosted Sep 12, 2012, 8:22 AM
I am having grid with templetfield in that template field dropdownlist is there i want that templatefield column create dyanamically as compare to entered id field
nallyaPosted Sep 12, 2012, 8:00 AM
http://www.c-sharpcorner.com/uploadfile/Shawpnendu/dynamically-creating-bound-and-template-columns-in-gridview-using-Asp-Net/
Dhanashri PatilPosted Sep 12, 2012, 7:41 AM
nallyaPosted Sep 12, 2012, 7:39 AM