如何从gridcolumn获取datetime值?

时间:2016-08-29 10:50:06

标签: c# winforms datetime devexpress filtering

我如何获取datetime值,因为我认为这里的问题是存储库值是字符串而不是日期值

  1. 我的网格栏
  2. enter image description here

    1. for repository item combobox
    2. enter image description here

      1. 当我选择它时,我可以过滤,因为值是字符串,证据在过滤器控制面板中,操作数是Begins with它应该是equal
      2. enter image description here

        1. 问题是当我按控制面板过滤时,我不能过滤
        2. enter image description here

          1. 通过过滤器控制输出
          2. enter image description here

            代码:

                RepositoryItemComboBox repositoryItemComboBox1 = new RepositoryItemComboBox();
            
                ci.DateTimeFormat.FullDateTimePattern = "dd MMM yyyy HH:mm:ss tt";
            
            private void gridView1_CustomRowCellEdit(object sender, CustomRowCellEditEventArgs e)
            {
                GridView view = sender as GridView;
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                  if (gridView1.GetDataRow(i) == null)
                  {
                    break;
                  }
            
                DateTime signIn1 = Convert.ToDateTime(gridView1.GetDataRow(i)["signIn"]);
                DateTime signIn_md = Convert.ToDateTime(signIn1.ToString(ci.DateTimeFormat.FullDateTimePattern));
            
                  if (!repositoryItemComboBox1.Items.Contains(signIn_md))
                  {
                     repositoryItemComboBox1.Items.Add(signIn_md);
                  }
            
                }
                  if (e.Column.FieldName == "signIn" && view.IsFilterRow(e.RowHandle))
                  {
                  e.RepositoryItem = repositoryItemComboBox1;
                  repositoryItemComboBox1.Sorted = true;    
                  }
            }
            
            
            private void filterControl1_BeforeShowValueEditor(object sender, DevExpress.XtraEditors.Filtering.ShowValueEditorEventArgs e)
            {
             if (e.CurrentNode.Property.Name == "signIn")
                        {
                            e.CustomRepositoryItem = repositoryItemComboBox9;
                        }
            }
            

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望将字符串转换为DateTime。

string strDate = "01 Aug 2016 15:31:07 PM";
DateTime dateTime = Convert.ToDateTime(strDate);

答案 1 :(得分:0)

为什么要将输入转换为DateTime两次?显然这里有一些错误:

DateTime signIn1 = Convert.ToDateTime(gridView1.GetDataRow(i)["signIn"]);
DateTime signIn_md = Convert.ToDateTime(signIn1.ToString(ci.DateTimeFormat.FullDateTimePattern));

也许您应该使用DateTime.ParseExact?既然您知道所需格式到底是什么,您可以用以下内容替换它:

DateTime signIn_md = DateTime.ParseExact(gridView1.GetDataRow(i)["signIn"], new String[] { "dd MMM yyyy HH:mm:ss tt" }, new System.Globalization.CultureInfo("en-US"), System.Globalization.DateTimeStyles.None);