gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:33:57 24 4
gpt4 key购买 nike

首先,我需要说声抱歉,因为我的英语不是很好,而且我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com