locked
How to manipulate data retrieved by SQLConnection? RRS feed

  • Question

  • User314929022 posted

    I am running a query that gives me 4 Pieces of Data

    • Name
    • Status
    • Elapsed Time in Status (in seconds)
    • Calls Received

    Currently I am populating a Gridview with the data using the below code and it works fine. However I am needing to edit the data on a finer scale once it is grabbed. I need to make the elapsed time show in hours, minutes and seconds instead of just seconds. I also need to section each row of results to display a different background color based on the status the user is currently in.

    I would like to be able to store each user from the query in a different table or grid so I can manipulate. Is this possible?

                SqlConnection con = new SqlConnection(ConnectionString);
                SqlDataAdapter dt = new SqlDataAdapter(strSQL, con);
                DataSet ds = new DataSet();
                dt.Fill(ds, "UserDetail");
                con.Close();
    
                GridView1.DataSource = ds;
    
                GridView1.DataBind();

    Tuesday, December 4, 2018 3:06 PM

All replies

  • User475983607 posted

    Use TimeSpan to format seconds into hour, minutes, and seconds.

    int seconds = 5000;
    TimeSpan ts = TimeSpan.FromSeconds(seconds);
    Console.WriteLine(ts.ToString());

    Or use math; 60 seconds in a minute, 60 minutes in an hours, etc.

    I don't understand the table requirement.  

    Tuesday, December 4, 2018 4:00 PM
  • User-271186128 posted

    Hi ASPnewbie751,

    On the GridView onRowDataBound event, you could use the following code to get the cell value:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Access Cell values.
            int customerId = int.Parse(e.Row.Cells[0].Text);
            string name = e.Row.Cells[1].Text;
            string country = e.Row.Cells[2].Text;
        }
    }

    or using the FindControl method to get the label or text control, then, get the value, code like this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Find the TextBox control.
            TextBox txtName = (e.Row.FindControl("txtName") as TextBox);
     
            //Find the DropDownList control.
            DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
            string country = (e.Row.DataItem as DataRowView)["Country"].ToString();
            ddlCountries.Items.FindByValue(country).Selected = true;
        }
    }

    Then, you could use the "TimeSpan.FromSeconds" method to change the value, finally, assign the value to the cell (control).

    More details about getting value from the GridView, please refer to these links:

    Find (Access) control inside GridView in RowDataBound and RowCommand events of ASP.Net GridView

    Get GridView cell value in RowDataBound and RowCommand events in ASP.Net

    Best regards,
    Dillion

    Wednesday, December 5, 2018 3:05 AM