Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview and a submit button(not in the code).

I want to show name, empno and current date and time in the gridview whenever I click submit button.
Also records should appear in a new row with current date and time.

I just need help with date and time + adding a new row.

How can I do it?
Please help.


XML
<asp:GridView ID="gv_addrow" runat="server" AutoGenerateColumns="False"
        CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="S.NO.">
                <ItemTemplate>
                   <%# ((GridViewRow) Container).RowIndex  + 1 %>
                </ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField HeaderText="ID ">

                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("EMPNO") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">

                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("NAME") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Checked Date">

                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server"></asp:Label>
                    <%=System.DateTime.Now%>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Time">

                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server"></asp:Label>
                        <%=System.DateTime.Now.Month %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>





Code :

HTML
public void ADD_getDetail()
    {
    


        SqlConnection con = new SqlConnection(s);
        con.Open();
        SqlCommand cmd = new SqlCommand("select EMPNO, NAME from zemp_s_web_al where EMPNO = " + "'" + EmpCode.Trim() + "'", con);
        //List<sqlparameter> paramList = new List<sqlparameter>();
        //paramList.Add(new SqlParameter("@EMPCODE", EmpCode));
        //cmd.Parameters.AddRange(paramList.ToArray());
        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataTable dt = new DataTable();
            da.Fill(dt);
     
        gv_addrow.DataSource = dt;
        gv_addrow.DataBind();
      
        con.Close();

    }</sqlparameter></sqlparameter>
Posted
Updated 4-Dec-11 23:05pm
v8
Comments
sriman.ch 5-Dec-11 4:05am    
Please be more specific.....
drstylo 5-Dec-11 4:10am    
whenever i click submit button , a new row should be added in the gridview with name , empcode ,current date and time
D K N T H 5-Dec-11 4:13am    
code tags added
sriman.ch 5-Dec-11 4:14am    
You want to add a new empty row with the same columns when you click on submit nutton is it correct ?
drstylo 5-Dec-11 4:18am    
yes but not the empty row , see i have a table with name and emp no , when i clicked submit button the gridview shows record now i want that whenever next time i enter a different id and click submit his record should appear in next row , right now it shows only one row , do you understand what i mean to say?

1 solution

Hi,

I will suggest 2 methods whichever you feel ok follow that
First Method:

1. Get the intital data into a dataset and place that dataset in cache or viewstate.
2. When you click on submit button then fetch the corresponding emp details into a datarow and add that row to the existibg dataset.
3. Now again bind the gridview with the updated dataset.

Second Method:

1. Write a dynamic sql query which will accpet employee no string separated by comma
2. In your code behind fetch all the emp id that are there in gridview plus the one which you have entered in textbox seperated by comma and paa this string as parameter to the sql query and get the results into dataset or datatable
3. Bind gridview with this datatble.


Hope this helps
 
Share this answer
 
Comments
drstylo 5-Dec-11 5:01am    
thanks
sriman.ch 5-Dec-11 5:02am    
Try on your own with this approach if you are facing any problems with writing code or sql query let me know
Dalek Dave 5-Dec-11 5:05am    
Good 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