本文介绍了用数据库中的数据填充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];
有关更多信息,请参见: www.dotnetperls/datagridview [ ^ ]
More info see: www.dotnetperls/datagridview[^]
我认为问题出在"GetDataSet"方法中. 在这里更改 I think the problem is in "GetDataSet" method. change here cmd.CommandType = CommandType.StoredProcedure
更多推荐
用数据库中的数据填充datagridview
发布评论