Click here to Skip to main content
15,941,071 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to create a gridview using a datasource from the code behind. I'm able to populate a basic gridview that just displays the information in bound columns, but I need the columns to be template fields. I've tried to put the exact number of template fields as there are columns but with no luck. With my sql statement and everything in code behind, how do I make it populate the Template fields?

Thanks in Advance
Sean8084

C#
protected void Page_Load (object sender, EventArgs e)
{
     if (total_Gridview.dataSource == null)
     {
          con.open();
          sql = (select statement);
          cmd = new SqlCommand(sql, con);
          total_Gridview.DataSource = cmd.ExecuteReader();
          total_Gridview.DataBind();
          con.close();
     }
}


ASP.NET
<asp:GridView ID="total_Gridview" runat="server" AutoGenerateColumns="False">
     <columns>
          <asp:TemplateField>
               <HeaderTemplate>
               </HeaderTemplate>
          
     </columns>

where my c# code says sql statement, it's a select statement, just can't disclose what it says.
that's the jist of it, if you need anymore, just let me know.

Thanks in advance
Sean8084
Posted
Updated 14-Sep-11 10:26am
v2
Comments
Herman<T>.Instance 14-Sep-11 14:09pm    
please show you code so we could see sharp how to help you with your problem
Sean8084 14-Sep-11 16:46pm    
I've added the code, hope it helps.

Depending on your "SELECT" statement you have to create the matching fields. If you select 4 fields in your select statement, you have to have matching fields in your gridview to show them. So before binding the gridview, you have to create the fields dynamically before binding.

For dynamically creating bound column you use the following code;

C#
BoundField nameColumn = new BoundField();
nameColumn.DataField = "Name";
nameColumn.HeaderText = "Person Name"; 
GridView1.Columns.Add(nameColumn);


For dynamically creating template column you use the following code;

C#
TemplateField ckhColumn = new TemplateField();
ckhColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "CheckBox Column");
ckhColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
GridView1.Columns.Add(ckhColumn);


Also have a look at this article in CP

How to create template columns dynamically in a grid view[^]

Good luck.
 
Share this answer
 
v2
Comments
kalsa 7-Jul-16 2:39am    
Nice...
Let me solve your problem.
for example you want to show for columns in the gried view fetched by sql query, your template fields will look like this in the griedview
XML
<Columns>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>


or you can copy and paste this code in between you gridview tag i.e.
<asp:GridView ID="GridView1" runat="server">
and
</asp:GridView>


It will create GridView with four Template Field four you.

Now for example your sql statement
is like " select Name, Address, Country, Qualification from tablename"

then add the below code to your gridview code(Just add Text property in all TextBox and Label field)

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>


and same in the

<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>


So your exact code for your one column will be like

XML
<asp:TemplateField HeaderText="User Id">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


In Bind("Pleae give Exact your Column Name")

Same add this code for all the column.

Please Mark it as Resolved if it resolved.

Please revert back to me in case of you find any doubt.

Thanks
Suman Zalodiya
 
Share this answer
 
v3
Comments
Sean8084 15-Sep-11 16:01pm    
thanks, your solution really helped a lot.
kalsa 7-Jul-16 2:40am    
Good
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                   ShowFooter="True" Height="137px"
                   Width="332px">
                   <Columns>
                    <asp:TemplateField HeaderText="sno">
                           <ItemTemplate>
                       <asp:Label ID="txtsno" runat="server" Text='<%# bind("sno") %>'></asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>

                       <asp:TemplateField HeaderText="Name of property">
                           <ItemTemplate>
                               <asp:Label ID="txtpropertyname" runat="server"
                                   Text='<%# Bind("Nameofproperty") %>'>&l
t;/asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>






then bind your code following way;

SqlConnection con=new SqlConnection(" U R connection string here:");
con.open();
SqlCommand cmd=new SqlCommand(" U R Query here.....",con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds,"a");
gridview1.DataSource=ds;
gridview1.DataBind();re>
 
Share this answer
 
v2
Comments
kalsa 7-Jul-16 2:40am    
Great
fghgfhgfh

thirumalai
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900