locked
column content in gridview based on two columns in "DataTable" RRS feed

  • Question

  • User-1849627223 posted

    Hello! 
    I need a hint. I have two columns in my GridView:

    <asp:BoundField DataField="Start_date" HeaderText="Start Date:" DataFormatString="{0:dd.MM.yy}" HtmlEncode="false" HeaderStyle-Width="50px" HeaderStyle-Wrap="False" ItemStyle-Wrap="False"></asp:BoundField>
    <asp:BoundField                        HeaderText="Planned start time:"  HeaderStyle-Width="60px" HtmlEncode="false" HeaderStyle-Wrap="False" ItemStyle-Wrap="False"></asp:BoundField>

    I have a Datatable object which contains columns "Start_date" and "Start_date_end" (DateTime), e.g. Start_date = 2010-01-13 08:00:00.000, Start_date_end = 2010-01-13 09:00:00.000

    I would like to show the data in the Griview like 
    Start Date    |    Planned start time:    
    01.13.10    |    08:00-09:00

    Planned start time is "time from Start_date"+"-"+"time from Start_date_end"

    How can I do this?

    Thank you, be well :)

    Friday, January 29, 2021 11:51 AM

All replies

  • User-1545767719 posted

    I suggest that you use the RowDataBound event of GridView.

    In its event handler , you can obtain the values of "Start_date" and "Start_date_end" (DateTime), change them to String while applying appropriate format, concatenate them to "time from Start_date"+"-"+"time from Start_date_end", and write the result to the text box.

    Friday, January 29, 2021 11:36 PM
  • User1535942433 posted

    Hi BSiedlecki,

    As far as I think,you could convert string to datetime and use HH:mm format.Just like this:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                     <Columns>
                       <asp:BoundField DataField="Start_date" HeaderText="Start Date:" DataFormatString="{0:dd.MM.yy}" HtmlEncode="false" HeaderStyle-Width="50px" HeaderStyle-Wrap="False" ItemStyle-Wrap="False"></asp:BoundField>
                       <asp:BoundField DataField="Start_date_end" HeaderText="Start Date:" DataFormatString="{0:dd.MM.yy}" HtmlEncode="false" HeaderStyle-Width="50px" HeaderStyle-Wrap="False" ItemStyle-Wrap="False"></asp:BoundField>
                       <asp:BoundField HeaderText="Planned start time:"  HeaderStyle-Width="60px" HtmlEncode="false" HeaderStyle-Wrap="False" ItemStyle-Wrap="False"></asp:BoundField>
                    </Columns>
                </asp:GridView>

    Code-Behind:

    protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Start_date", typeof(DateTime)));
                dt.Columns.Add(new DataColumn("Start_date_end", typeof(DateTime)));
                dt.Rows.Add("2010-01-13 08:00:00.000", "2010-01-13 09:00:00.000");
                if (!IsPostBack)
                {
                    ViewState["datatable"] = dt;
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    DataTable dte = (DataTable)ViewState["datatable"];
                    foreach (DataRow dr in dte.Rows)
                    {
                        string x = Convert.ToDateTime(dr["Start_date"]).ToString("hh:mm");
                        string y = Convert.ToDateTime(dr["Start_date_end"]).ToString("hh:mm");
                        e.Row.Cells[2].Text = x + "-" + y;
                    }
    
                }
            }

    Best regards,

    Yijing Sun

    Monday, February 1, 2021 6:28 AM