locked
converting text in gridview label into time format RRS feed

  • Question

  • User-1872583635 posted

    Summary of the problem I am having:

    hi all,

    I got one function to insert data from dropdownlist (datetime format) into textbox in gridview.

    how do I want to display only TIME not DATE on the textbox that I insert data from selected dropdownlist.

    Error I am receiving:

    data insert

    TIME : 9:00 AM

    result in textbox

    TIME: 12/30/1899 9:00:00 AM

    result expected in textbox

    TIME : 9:00 AM

    My code:

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="TIME" DataValueField="TIME" DataTextFormatString="{0:hh:mm tt}"></asp:DropDownList>

    Code Behind:

    protected void Button2_Click(object sender, EventArgs e)

    {

    foreach (GridViewRow row in GridView1.Rows)

    {

    if (row.RowType == DataControlRowType.DataRow)

    {

    TextBox Time = (TextBox)row.FindControl("TextBox15");

    Time.Text = DropDownList1.Text; }}

    Please help me.

    Thank You :)

    Thursday, July 16, 2020 11:12 AM

Answers

  • User288213138 posted

    Hi yura.s,

    I think I understand why it appear like that (like this "12/30/1899 9:00:00 AM") it is due to my textbox in the gridview is in text form while my dropdownlist is in DateTime form.

    so how can i convert my textbox15 in the gridview to read as datetime format?

    Time.Text = DropDownList1.Text;

    Have you used my code above?  this code can convernt string to datetime format.

    Time.Text = Convert.ToDateTime(DropDownList1.Text).ToString("h :mm tt");     

    You can also set DataTextFormatString="{0:h:mm tt}" in the dropdownlist, so that the DropDownList displays directly with 9:00 AM.

     <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="TIME"
    DataTextFormatString="{0:h:mm tt}"></asp:DropDownList>

    This is my tested code, 

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="TIME"></asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ThreadDBConnectionString %>" SelectCommand="SELECT [TIME] FROM [Date]"></asp:SqlDataSource>
                <asp:GridView ID="GridView1" runat="server">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox15" runat="server"></asp:TextBox>
                            </ItemTemplate>                       
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
    protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox Time = (TextBox)row.FindControl("TextBox15");
                        Time.Text = Convert.ToDateTime(DropDownList1.Text).ToString("h :mm tt");              
                    }
                }
            }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2020 8:49 AM

All replies

  • User288213138 posted

    Hi yura.s,

    how do I want to display only TIME not DATE on the textbox that I insert data from selected dropdownlist.

    Error I am receiving:

    data insert

    TIME : 9:00 AM

    result in textbox

    TIME: 12/30/1899 9:00:00 AM

    result expected in textbox

    TIME : 9:00 AM

    According to your description, I cannot reproduce your problem.

    Do you want to display 9:00 AM from 12/30/1899 9:00:00 AM in Textbox?

    If so, you can try below code:

    foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox Time = (TextBox)row.FindControl("TextBox15");
                        Time.Text = Convert.ToDateTime(DropDownList1.Text).ToString("h :mm tt");              
                    }
                }

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    Friday, July 17, 2020 3:04 AM
  • User-1872583635 posted

    hi Sam,

    thanks for your reply.

    what you understand is correct. but I still cannot make it according to your suggestion.

    I think I understand why it appear like that (like this "12/30/1899 9:00:00 AM") it is due to my textbox in the gridview is in text form while my dropdownlist is in DateTime form.

    so how can i convert my textbox15 in the gridview to read as datetime format?

    DROPDOWNLIST1 INPUT (TIME) = 9:00 AM

    DROPDOWNLIST2 INPUT (DEPT)= ELECTRICAL

    IN GRIDVIEW1 EXPECTED OUTPUT:

    |      A      |       B       |     TIME      |       DEPT      |

    |  DATA1 |  DATA 2  |  9:00 AM    | ELECTRICAL |

    INSTEAD IT SHOWS LIKE THIS

    IN GRIDVIEW1 OUTPUT:

    |      A      |       B       |                TIME                 |       DEPT      |

    |  DATA1 |  DATA 2  |  12/30/1899 9:00:00 AM  | ELECTRICAL |

    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="DEPT" DataValueField="DEPT"></asp:DropDownList>
    
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:eMovementConnectionString %>" ProviderName="<%$ ConnectionStrings:eMovementConnectionString.ProviderName %>" SelectCommand="SELECT [DEPT] FROM [DEPT]"></asp:SqlDataSource>
    
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="TIME" DataValueField="TIME" DataTextFormatString="{0:h:mm tt}" >
    
    </asp:DropDownList>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:eMovementConnectionString %>" ProviderName="<%$ ConnectionStrings:eMovementConnectionString.ProviderName %>" SelectCommand="SELECT [TIME] FROM [TIME]"></asp:SqlDataSource>
    
    <asp:Button ID="Button3" runat="server" Text="Insert to Grid" OnClick="Button2_Click" />
    
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found. Pls check the key in info">
    
    
    <Columns>
    
    <asp:TemplateField HeaderText="A">
    
    <ItemTemplate>
    
    <asp:Label ID="LabelA" runat="server" Text='<%#Bind("A") %>'></asp:Label> 
    
    </ItemTemplate>
    
    </asp:TemplateField> 
    
    <asp:TemplateField HeaderText="B">
    
    <ItemTemplate>
    
    <asp:Label ID="LabelB" runat="server" Text='<%#Bind("B") %>'></asp:Label> 
    
    </ItemTemplate>
    
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="DEPT">
    
    <ItemTemplate>
    
    <asp:TextBox ID="TextBox14" runat="server"></asp:TextBox>
    
    </ItemTemplate>
    
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="Time">
    
    <ItemTemplate>
    
    <asp:TextBox ID="TextBox15" runat="server"></asp:TextBox>
    
    </ItemTemplate>
    
    </asp:TemplateField>
    
    protected void Button2_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        
                        TextBox Dept = (TextBox)row.FindControl("TextBox14");
                        TextBox Time = (TextBox)row.FindControl("TextBox15");
                        Dept.Text = DropDownList2.Text;
                        Time.Text = DropDownList1.Text;
                                            
                    }
                }

    hope this will help.

    tq so much

    Monday, July 20, 2020 12:08 PM
  • User288213138 posted

    Hi yura.s,

    I think I understand why it appear like that (like this "12/30/1899 9:00:00 AM") it is due to my textbox in the gridview is in text form while my dropdownlist is in DateTime form.

    so how can i convert my textbox15 in the gridview to read as datetime format?

    Time.Text = DropDownList1.Text;

    Have you used my code above?  this code can convernt string to datetime format.

    Time.Text = Convert.ToDateTime(DropDownList1.Text).ToString("h :mm tt");     

    You can also set DataTextFormatString="{0:h:mm tt}" in the dropdownlist, so that the DropDownList displays directly with 9:00 AM.

     <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="TIME"
    DataTextFormatString="{0:h:mm tt}"></asp:DropDownList>

    This is my tested code, 

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="TIME"></asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ThreadDBConnectionString %>" SelectCommand="SELECT [TIME] FROM [Date]"></asp:SqlDataSource>
                <asp:GridView ID="GridView1" runat="server">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:TextBox ID="TextBox15" runat="server"></asp:TextBox>
                            </ItemTemplate>                       
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
    protected void Button1_Click(object sender, EventArgs e)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox Time = (TextBox)row.FindControl("TextBox15");
                        Time.Text = Convert.ToDateTime(DropDownList1.Text).ToString("h :mm tt");              
                    }
                }
            }

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2020 8:49 AM