用数据库中的数据填充datagridview

编程入门 行业动态 更新时间:2024-10-22 08:03:33
本文介绍了用数据库中的数据填充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

本文发布于:2023-08-05 21:28:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1308153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   数据   datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!