C# 需要将 sql 中的数据放入 DataGridView 并按行的日期排序

标签 c# sql winforms datagridview

首先,我需要说声抱歉,因为我的英语不是很好,而且我是 C# 的新手。 我有一个关于如何从我的数据库中仅获取“Summ”值并将其按“mm”和“yy”排序并排序的问题,谢谢帮助。

数据库:

enter image description here

我有一个关于如何从我的数据库中仅获取“Summ”值并将其按“mm”和“yy”排序并排序的问题谢谢你的帮助。这是我的设计 View (不能放图片)我只需要这样做 :D

enter image description here

代码:

            DateTime endtime = new DateTime();

            DateTime starttime = new DateTime();

            endtime = dateTimePicker2.Value;
            starttime = dateTimePicker1.Value;
            int mount = (((endtime.Year - starttime.Year) * 12) + (endtime.Month + 1) - starttime.Month);

            textBox1.Text = mount.ToString();

string mm,yy;
            string inp;
            inp = textBox1.Text;
            int dt;
    dt = dataGridView1.Columns.Count;


            int.TryParse(inp, out dt); 
            dataGridView1.Columns.Clear();
            //sql connect

            SqlConnection coco = new SqlConnection("Data Source=192.168.9.11;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=;");
            coco.Open();
            SqlCommand comm = new SqlCommand("SELECT Summ FROM test_recv Where mm like"+mm+"yy like "+yy+"");
     try
            {
                coco.Open();
               SqlDataReader reader = comm.ExecuteReader();

                    while (reader.Read())
                    {
                      string  Summ = reader.GetString(0);
                    }

                reader.Close();
                coco.Close();
            }
            catch (Exception)
            {
                if (coco.State == ConnectionState.Open)
                    coco.Close();
            }    


    for (int x = 0; x < dt; x++)
                {
mm = starttime.AddMonths(x).ToString("MM", seCultureInfo);
yy = starttime.AddMonths(x).ToString("yyyy", seCultureInfo);
                    dataGridView1.Columns.Add("A", starttime.AddMonths(x).ToString("MM", seCultureInfo) + "/" + starttime.AddMonths(x).ToString("yyyy", seCultureInfo));
dataGridView1.Rows[0].Cells[x].Value = Summ;


}

最佳答案

在您想要获取数据的地方使用此代码并将结果放在网格列而不是行中:

var connection = new System.Data.SqlClient.SqlConnection(@"Your Connection String");

//Your command: SELECT yy+mm AS yymm, Summ FROM test_recv /*WHERE...*/ ORDER BY yymm ASC
//Add Whatever WHERE clause you need
//Pay attention that yy+mm Selected at first and Summ selected at seccond position
//Pay attention we ORDER BY yymm ASC
var command = new System.Data.SqlClient.SqlCommand("Your Command", connection);

try
{
    this.dataGridView1.Columns.Clear();
    this.dataGridView1.Rows.Clear();
    connection.Open();
    var reader = command.ExecuteReader();
    int columnIndex = 0;
    while (reader.Read())
    {
        //0 is Index of Summ
        var header = reader.GetString(0);

        //1 is Index of yymm
        var value= reader.GetString(1);

        var column = new DataGridViewTextBoxColumn();
        column.HeaderText = header;
        this.dataGridView1.Columns.Add(column);
        if(dataGridView1.RowCount==0)
            this.dataGridView1.Rows.Add(1);

        this.dataGridView1.Rows[0].Cells[columnIndex].Value = value;

        columnIndex++;
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    if (connection.State == ConnectionState.Open)
        connection.Close();
}

这里是测试数据

Summ    mm      yy      
------------------------
10000   08      2015
15000   09      2015      
8000    10      2015
20000   11      2015
5000    12      2015

这是截图:

enter image description here

关于C# 需要将 sql 中的数据放入 DataGridView 并按行的日期排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32240318/

相关文章:

c# - 在单元测试中为 ApiController 设置用户属性

c# - C# 中的 Async 和 Await 未正确理解

c# - 全局异常捕获仅在调试 WinForms 应用程序时有效

php - 大型数据库的性能,更快的方法 : finding value with php or sql?

php - 用户收入不会更高

c# - 如何使 Winforms 表单终止应用程序?

c# - 创建图像列表框?

c# - 我可以在 UWP 上运行一些 http 服务器吗?

php - Mysql 比较行之间的嵌套数据

c# - 对父窗体或目标控件使用 Invoke 之间有什么区别吗?