Click here to Skip to main content
14,271,013 members
Rate this:
Please Sign up or sign in to 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

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: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
digimanus 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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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;

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;

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.
   
v2
Comments
kalsa 7-Jul-16 2:39am
   
Nice...
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
<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

<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
   
v3
Comments
Sean8084 15-Sep-11 16:01pm
   
thanks, your solution really helped a lot.
kalsa 7-Jul-16 2:40am
   
Good
Rate this:
Please Sign up or sign in to vote.

Solution 3

<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>
   
v2
Comments
kalsa 7-Jul-16 2:40am
   
Great
Rate this:
Please Sign up or sign in to vote.

Solution 4

fghgfhgfh

thirumalai
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100