2
public partial class logRead : Form
{
    DataSet ds = new DataSet();
    DataTable tab = new DataTable();

    public logRead()
    {
        InitializeComponent();
    }
    string line;
    private void BtnUser_Click(object sender, EventArgs e)
    {           
        DataRow[] filteredRows = tab.Select("Username = '"+cmbUsername.Text+"'");
        DataTable dt = filteredRows.CopyToDataTable();
        dgv1.DataSource = dt;
        txtcount.Text = dgv1.Rows.Count.ToString();         
    }

    private void btnsearch_Click(object sender, EventArgs e)
    {
        dtmDate.CustomFormat = "dd MMM yyyy hh mm ss";
        DataRow[] filteredRows = tab.Select("Datetime = '" +dtmDate.Text + "'");
         DataTable dt = filteredRows.CopyToDataTable();
        dgv1.DataSource = dt;                   
    }

    private void logRead_Load(object sender, EventArgs e)
    {
        StreamReader strRead = new StreamReader("D:\\login.hml");
        string line;
        line = strRead.ReadToEnd();
        DataRow row = null;
        tab.Columns.Add("Ipaddress");
        tab.Columns.Add("Sysname");
        tab.Columns.Add("Username");
        tab.Columns.Add("Text");
        tab.Columns.Add("Datetime");
        string[] rows = line.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string r in rows)
        {
            string[] columns = r.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (columns.Length <= tab.Columns.Count)
            {
                row = tab.NewRow();

                for (int i = 0; i < columns.Length; i++)

                    row[i] = columns[i];
                tab.Rows.Add(row);
            }
        }
        ds.Tables.Add(tab);
        dgv1.DataSource = ds.Tables[0];
        txtcount.Text = dgv1.Rows.Count.ToString();
    }
}}

我有一个日志文件,当表单加载本身时,我编写代码来填充 datagridview,我使用临时数据表和数据集并填写到 datagridview,我使用组合框临时填充所有用户名,如果我单击组合框中的任何名称,那么所有名称是什么信息填写 datagridview 我在 btn userclik 中编写该代码现在我想要按月或日期记录表我采取了两个组合框和一个组合框填充日期时间选择器和一个多月我填写,现在我想点击和月份我想显示月份在datagridview中明智的数据,我写了代码搜索点击我没有得到。(dis在Windows窗体中)还有一个我没有使用数据库,请检查代码。

4

2 回答 2

1

而不是使用 forDataTable.Select我会DateTime 在 Linq-To-DataSet 查询中使用。您可以使用DateTime.TryParseExact将字符串解析为DateTime.

var formatPattern = "dd MMM yyyy hh mm ss";
DateTime parsedDate;
var culture = System.Globalization.CultureInfo.InvariantCulture; // use CurentCulture if you want to use the current culture instead which might change
bool success = DateTime.TryParseExact(dtmDate.Text, formatPattern, culture, DateTimeStyles.None, out parsedDate);
if (success)
{ 
    // assuming you want all rows of the same day not the same second:
    var filteredRows = tab.AsEnumerable()
        .Where(r => r.Field<DateTime>("Datetime").Date == parsedDate.Date);
    dgv1.DataSource = filteredRows.CopyToDataTable();
}
于 2013-01-31T12:01:24.873 回答
1
var formatPattern = "dd MMM yyyy hh mm ss";
DateTime parsedDate;
var culture = System.Globalization.CultureInfo.InvariantCulture; // use CurentCulture if you want to use the current culture instead which might change
bool success = DateTime.TryParseExact(dtmDate.Text, formatPattern, culture, DateTimeStyles.None, out parsedDate);
if (success)
于 2013-03-26T12:11:48.457 回答