简体   繁体   English

在DetailsView中的DateTime中需要毫秒精度c#如何在Details视图中直接访问数据?

[英]Need Millisecond Accuracy in DateTime in DetailsView c# How to Access Data directly in Details View?

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. 因此,我一直在浏览堆栈溢出和MSDN,无法找到控件(或弄清楚我拥有的控件)来直接访问detailsview的数据。 I'm in C# using a .Net WebApplication. 我在使用.Net WebApplication的C#中。

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? 我认为我正在寻找的是gridview中的等效项是row.Cells [1] .Value有人可以帮助访问DetailsView单元格的值吗?

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. 我想做的是访问我绑定到DetailsView1的确切数据值。对于所有数字和字符串(例如仅显示两个),Text就足够了,但对于时间戳MTTS(日期时间)却是不够的,因为它丢失了毫秒和代码(SQL查询)之后使用的代码,如果没有毫秒就无法在db中找到正确的值。 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. 但是它无法识别MTTS,因此我不确定如何设置参数。我已经尝试了几种不同的方法,但均未成功。

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. 解决方法是混乱的,从本质上来说,我重建了将数据收集到GridView的查询,然后我使用LinQ和从GridView内部的参数(直接将MTTS分配为DateTime)获取了直接捕获MTTS的功能。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM