用数据库中的数据填充datagridview [英] Fill datagridview with data from database
本文介绍了用数据库中的数据填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
单击按钮后,我将从数据库中填充datagridview.我正在使用存储过程检索要在datagridview中显示的数据列
存储过程
I''m filling the datagridview from database on button click. I''m retrieving the column of data to be displayed in datagridview using stored procedure
Stored procedure
CREATE PROCEDURE [dbo].[prcDisplaydataTemplate]
AS
BEGIN
(SELECT DataTable.objectOriginalName FROM dbo.DataTable)
END
按钮单击事件
Button Click Event
private void btDataTemplate_Click(object sender, EventArgs e)
{
DataSet dsobbtemp = new DataSet();
DBConnection objConnection = new DBConnection();
try
{
objConnection.GetConnection();
dsobbtemp = objConnection.GetDataSet("prcDisplaydataTemplate");
dgDtParameters.DataSource = dsobbtemp;
}
catch (Exception)
{
throw;
}
finally
{
if (objConnection.sconnConnection.State == ConnectionState.Open)
{
objConnection.CloseConnection();
}
}
}
GetConnection方法
GetConnection Method
public void GetConnection()
{
try
{
sconnConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString());
sconnConnection.Open();
}
catch (SqlException) { throw; }
}
GetDataSet方法
GetDataSet Method
public DataSet GetDataSet(string sql)
{
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand(sql, this.sconnConnection);
adapter.SelectCommand = cmd;
adapter.Fill(dataset);
return dataset;
}
我出错了,我的datagridview显示为蓝色,没有任何数据
在此先感谢
Where I have went wrong my datagridview displays in blue color without any data
Thanks in Advance
推荐答案
dgDtParameters.DataSource = dsobbtemp.Tables[0];
有关更多信息,请参见: http://www.dotnetperls.com/datagridview [
More info see: http://www.dotnetperls.com/datagridview[^]
我认为问题出在"GetDataSet"方法中.
在这里更改
I think the problem is in "GetDataSet" method.
change here
cmd.CommandType = CommandType.StoredProcedure
这篇关于用数据库中的数据填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文