Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have table in database. I am fetching the records of that table and displaying it in a gridview. In this the column names are same as the that of the column names in databse table.

I need to change the name of colums in gridview. Can anyone help me ?

C#
Protected void Page_Load(object sender, EventArgs e) 
{ 
SqlCommand cmd = new SqlCommand("select username,Level1_Attempt1,Level1_Attempt2,Level1_Attempt3,Level2_Attempt1,Level2_Attempt2,Level2_Attempt3,Level3_Attempt1,Level3_Attempt2,Level3_Attempt3 from register", con); 
con.Open(); 
SqlDataReader sdr = cmd.ExecuteReader(); 
GridView1.DataSource = sdr; 
GridView1.DataBind(); 
con.Close(); 
} 


I need to change the names level1_attempt1,level1_attempt2,.......and put required names of my interest in gridview
Posted
Updated 24-Jul-13 1:11am
v3
Comments
mskphani 24-Jul-13 5:44am    
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select username,Level1_Attempt1,Level1_Attempt2,Level1_Attempt3,Level2_Attempt1,Level2_Attempt2,Level2_Attempt3,Level3_Attempt1,Level3_Attempt2,Level3_Attempt3 from register", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
GridView1.DataSource = sdr;
GridView1.DataBind();
con.Close();
}


i need to change the names level1_attempt1,level1_attempt2,.......and put required names of my interest in gridview
Rajeev Jayaram 24-Jul-13 7:06am    
Use the "Improve question" option to update your question.

You can use itemtemplates and template fields to bind the database colums with grid:

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="10">
 <Columns>
                        <asp:TemplateField HeaderText="ID">
                            <ItemTemplate>
                                <asp:Label ID="lblid" runat="server"   Text= '<%#Eval("id")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>


                        <asp:TemplateField HeaderText="Date">
                            <ItemTemplate>
                                 <%#Eval("date")%>
                            </ItemTemplate>
                        </asp:TemplateField>



                         <asp:TemplateField HeaderText="Time In">
                            <ItemTemplate>
                                 <%#Eval("timein")%>
                            </ItemTemplate>
                        </asp:TemplateField>

                         <asp:TemplateField HeaderText="Time Out">
                            <ItemTemplate>
                                 <%#Eval("timeout")%>
                            </ItemTemplate>
                        </asp:TemplateField>

                         <asp:TemplateField HeaderText="Status">
                            <ItemTemplate>
                                 <%#Eval("time_status")%>
                            </ItemTemplate>
                        </asp:TemplateField>


                       <asp:TemplateField HeaderText="Reason">
                            <ItemTemplate>
                                <asp:TextBox ID="txtreason" TextMode="MultiLine" Width="300" Height="100" Text= '<%#Eval("reason")%>' runat="server"></asp:TextBox>

                            </ItemTemplate>
                        </asp:TemplateField>


                        <asp:TemplateField HeaderText="Grant Status">
                            <ItemTemplate>
                                 <%#Eval("granted")%>
                            </ItemTemplate>
                        </asp:TemplateField>


                        <asp:TemplateField >
                <ItemTemplate>
                  <asp:LinkButton ID="lnkbtnUpdate" runat="server"  Text="Send Cancel Req" CommandName="Update"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
                    </Columns>
 </asp:GridView>
 
Share this answer
 
v3
Logically
C#
dataGridView1.Columns[id].HeaderText = "your desire name";
would do the trick.
See and example here..
http://stackoverflow.com/questions/13395321/change-column-name-in-datagridview[^]
 
Share this answer
 
Based on the solutions given,
I would say No.2 is the only one which helps best in your case.

However you should not do it that way.
Don't forward the problem when you can fix it at its root.

SQL
--Rename your columns right at your sql statement:
select username AS 'Username', Level1_Attempt1 AS 'First Level1 Attempt'..... FROM register
 
Share this answer
 
XML
<asp:BoundField HeaderText="Name" DataField="Name" HeaderStyle-Height="25" ItemStyle-HorizontalAlign="left"
                      ItemStyle-Width="20%" SortExpression="Name">
                      <HeaderStyle Height="25px" />
                      <ItemStyle Width="20%" />
                      </asp:BoundField>
 
Share this answer
 
Comments
Rajeev Jayaram 24-Jul-13 7:13am    
You want to tell something about the code?

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