Question

So I have been browsing stack overflow and MSDN and cannot find a control (or make sense of the ones I have) to access the data directly of a detailsview. I'm in C# using a .Net WebApplication.

I think what I am looking for is the equivalent in gridview is row.Cells[1].Value can anybody help with the accessor to the DetailsView cells?

What I am trying to do is to access the exact data values I have bound to the DetailsView1 .Text is sufficient for all the numbers and string (only two shown for example) but not for the timestamp MTTS (a datetime) as it lost the milliseconds and the code (SQL query) I use after it cannot find the correct values in the db without the milliseconds. Will I also need to change the way I have bound the data, or some setting to give the bound data millisecond accuracy?

Code example:

    Decimal RUN_ID = 0;
    DateTime MTTS = new DateTime();

    foreach(DetailsViewRow row in DetailsView1.Rows)            
    {                
            switch(row.Cells[0].Text)
            {

                case "RUN_ID":
                    RUN_ID = Decimal.Parse(row.Cells[1].Text);
                    break;
                case "MTTS":
                    MTTS = DateTime.Parse(row.Cells[1].ToString());                       
                    break;

           }

   }

I have tried

   row.Cells[1].ID = "MTTS";
   MTTS = (DateTime)((DataRowView)DetailsView1.DataItem)["MTTS"];

But it does not recognize the MTTS and I am not sure how to set the parameter I have tried a few different things already with no success.

Was it helpful?

Solution

The workaround was messy, essentially I rebuilt the query that gathered the data to the GridView and then I made a function to grab the MTTS directly using LinQ and the parameteres from inside the GridView which assigns the MTTS as a DateTime.

This was in my opinion a bad way of doing things but it worked. I would prefer a better solution.

    MTTS = GetMTTS(JOB_PLAN, JOB_NAME,JOB_NAME_ID,RUN_ID,JOB_STATUS);

    public DateTime GetMTTS(string JOB_PLAN, string JOB_NAME, string JOB_NAME_ID, Decimal RUN_ID, string JOB_STATUS){

        string myEnvName = XXX;
        TableName = XXX.ToString();
        ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[myEnvName].ToString();

        string thisRUN_ID = RUN_ID.ToString();
        cmdText = @"SELECT MTTS FROM " + TableName + 
            " WHERE JOB_PLAN = '" + JOB_PLAN + "'"
            + " AND JOB_NAME = '" + JOB_NAME + "'"
            + " AND JOB_NAME_ID = '" + JOB_NAME_ID + "'"
            + " AND RUN_ID = '" + thisRUN_ID + "'"
            + " AND JOB_STATUS = '" + JOB_STATUS + "'";

        DataSet ds = new DataSet();           
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            conn.Open();
            try
            {
                SqlCommand SQLcc = new SqlCommand(cmdText,conn);
                SqlDataReader reader;
                reader = SQLcc.ExecuteReader();
                while (reader.Read())
                {
                    MTTS = reader.GetDateTime(0);
                }        
                reader.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }                
        }
        return MTTS;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top