2

我对 Windows 窗体(.net 3.5)很陌生,并试图以一种好的方式呈现一些绑定数据。

我有一个看起来像这样的表:

date        -      unit     -      message
2012-01-01         200             some text
2012-01-01         300             some text
2012-01-01         400             some text
2012-01-02         100             some text
2012-01-02         300             some text
2012-01-02         700             some text

我想将其输出到:

units       -    2012-01-01 - 2012-01-02   - count
200              some text                   1
100                           some text      1
300              some text    some text      2

等等……这可能吗?我应该从哪里开始?我不知道所有可用的功能和控件,也许你们有一些聪明的想法。

提前致谢!

代码:

public DataTable ReadExcel()
{
        String excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=test.xlsx;Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"";
        OleDbConnection excelCon = new OleDbConnection(excelConnectionString);
        OleDbCommand excelSelectCmd = new OleDbCommand("SELECT * FROM [stock$]", excelCon);
        OleDbDataAdapter excelAdapter = new OleDbDataAdapter();
        excelAdapter.SelectCommand = excelSelectCmd;
        DataSet excelDS = new DataSet();
        DataTable dt = new DataTable();
        excelAdapter.Fill(excelDS);
        excelCon.Close();

         return dt;
}

所以基本上我只是从一个excel文件中填充一个数据表并返回它,然后我将它绑定到一个DataGridView,数据在网格中按预期显示。

现在我只是在寻找一种以另一种方式呈现数据的方法。

        DBLayer test = new DBLayer();
        dataGridView1.DataSource = test.ReadExcel();
4

2 回答 2

3

希望它会有所帮助。

EDIT5我已将绑定添加到 dataGridView

public static class GroupedDtoWorker
{
    public static List<GroupedDto> GetIt()
    {
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("date").DataType = typeof(DateTime);
        dataTable.Columns.Add("unit").DataType = typeof(Int32);
        dataTable.Columns.Add("message").DataType = typeof(String);
        dataTable.Rows.Add("27.5.1989", "200", "someText");
        dataTable.Rows.Add("27.6.1989", "300", "someText");
        dataTable.Rows.Add("27.7.1989", "400", "someText");
        dataTable.Rows.Add("27.8.1989", "100", "someText");
        dataTable.Rows.Add("27.9.1989", "300", "someText");
        dataTable.Rows.Add("27.10.1989", "700", "someText");

        var result = (from rows in dataTable.AsEnumerable()
                      select new Dto
                      {
                          DateTime = rows.Field<DateTime>("date"),
                          Unit = rows.Field<Int32>("unit"),
                          Message = rows.Field<String>("message")

                      }).GroupBy(e => e.Unit)
                            .Select(
                                e =>
                                new GroupedDto()
                                {
                                    DateMessage = GetDictionary(e),
                                    Unit = e.AsQueryable().First().Unit,
                                    Count = e.Count()
                                }).ToList();
        return result;
    }

    public static Dictionary<String, String> GetDictionary(IGrouping<int, Dto> input)
    {
        Dictionary<String, String> result = new Dictionary<string, string>();
        input.ToList().ForEach(e => result.Add(e.DateTime.Date.ToString("yyyy/MM/dd"), e.Message));
        return result;
    }

}

public class GroupedDto
{
    public Int32 Unit { get; set; }
    public Dictionary<String, String> DateMessage { get; set; }
    public Int32 Count { get; set; }
}

public class Dto
{
    public DateTime DateTime { get; set; }
    public Int32 Unit { get; set; }
    public String Message { get; set; }
}

和 DataGridView 绑定:

    public Form1()
    {
        InitializeComponent();
        var dto = GroupedDtoWorker.GetIt();
        dataGridView.Columns.Add("units", "units");
        foreach (GroupedDto groupedDto in dto)
        {
            foreach (KeyValuePair<string, string> keyValuePair in groupedDto.DateMessage)
            {
                if (!dataGridView.Columns.Contains(keyValuePair.Key))
                {
                    dataGridView.Columns.Add(keyValuePair.Key, keyValuePair.Key);
                }
            }
        }
        dataGridView.Columns.Add("count", "count");

        foreach (GroupedDto groupedDto in dto)
        {
            String[] row = new string[dataGridView.Columns.Count];
            row[0] = groupedDto.Unit.ToString();
            row[dataGridView.Columns.Count - 1] = groupedDto.Count.ToString();
            foreach (KeyValuePair<string, string> keyValuePair in groupedDto.DateMessage)
            {
                Int32 index = dataGridView.Columns[keyValuePair.Key].Index;
                row[index] = keyValuePair.Value;
            }
            dataGridView.Rows.Add(row);
        }
    }
于 2013-02-21T10:16:24.147 回答
0

如果您希望使用 LINQ 查询数据表,那么您最好执行以下操作:-

public class Dto
{
    public DateTime dateTime { get; set; }
    public Int32 Unit { get; set; }
    public String Message { get; set; }
}


public List<Dto> GetEnumerableObject()
{

       //Populate your Datatable from Excel Spreadsheet here as your already doing


       Var Result = (from rows in DataTable.AsEnumerable()

                     select new Dto
                     {
                        dateTime = rows.Field<DateTime>("date"),
                        Unit = rows.Field<Int32>("unit"),
                        Message = rows.Field<string>("message")

                     }).ToList();

       return Result;

}

在您的主代码中,您可以执行以下操作:-

List<Dto> myList = GetEnumerableObject(); 

然后你就可以学习LINQ的乐趣了。

于 2013-02-21T11:14:36.457 回答