从数据库绑定datagridview? [英] Bind datagridview from database?

查看:76
本文介绍了从数据库绑定datagridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有成员,

我想从数据库中绑定DatagridView,就像我在Windows应用程序中设计的附件设计

一样。 C#。

如何???? (不是手动的)。

请帮助。



i我正在使用该代码。

Dear all members,
I want to Bind DatagridView from database just like my Attachment designed
in Windows Application. C#.
How???? (not manual).
Please Help.

i am using that code.

private void binddata()
{
SqlDataAdapter da = new SqlDataAdapter(@"SELECT distinct Sectiontype + ' ' + '['+ Questiontype + ']' + ' ' + 'Attempt Question :-' + ' ' + Attemptquestion + ' ' + 'Total Marks :-' + ' ' + Totalmarks
as 'Groupst', Question,Fullmarks FROM dbo.tbl_QuestionConfig where Subject='C++'
group by Sectiontype + ' ' + '['+ Questiontype + ']' + ' ' + 'Attempt Question :-' + ' ' +
Attemptquestion + ' ' + 'Total Marks :-' + ' ' + Totalmarks ,Question,Fullmarks", con);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AllowUserToAddRows = false;

dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.LightSeaGreen;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
r.DefaultCellStyle.BackColor = Color.LightCyan;
}


} 







< a href =https://postimg.org/image/55zvylwpn/>点击这里



请看链接。

i试过但不能。



我尝试了什么:



推荐答案

dataGridView1.AutoGenerateColumns = false;



这是您的问题如果您的表单中没有dataGridView1作为数据集的列在编译时返回。如果这样更改此行为


Here is your problem If you have not dataGridView1 in your Form designed with the columns as your datatabe returns at compile time.If so change this line as

dataGridView1.AutoGenerateColumns = true;


参见如何:将数据绑定到Windows窗体DataGridView控件 [ ^ ]。


这篇关于从数据库绑定datagridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆