我的存储过程存在问题。
此代码有效(使用ListBox
)
private void button4_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
string sqlCmd = "Drie duurste producten";
SqlCommand cmd = new SqlCommand(sqlCmd, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = sqlCmd;
connection.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
listBox1.Items.Add(reader.GetValue(0).ToString());
}
}
connection.Close();
}
但是如何将此数据添加到DataGridView
而不是ListBox
?
谢谢!
答案 0 :(得分:3)
更改为
......
using (SqlDataAdapter adapter = new SqlDataAdapter())
{
DataTable dt = new DataTable();
adapter.SelectCommand = cmd; {
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
......
通常会填充DataGridView,将完整的数据源绑定到其DataSource属性,并让控件找出如何配置其列以及显示的值的格式
答案 1 :(得分:1)
SqlDataAdapter是最简单的方法。
但也可以创建DataTable并手动填充它并将DataGridView的DataSource值分配给DataTable实例:
...
DataTable dt = new DataTable("test");
dt.Columns.Add("test");
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
DataRow dr = dt.NewRow();
dr[0] = reader.GetValue(0).ToString();
dt.Rows.Add(dr);
}
}
dataGridView1.DataSource = dt;
....
答案 2 :(得分:1)
static public long Insert(BillAO ao)
{
try
{
SqlParameter[] Params =
{
new SqlParameter("@Status",ao.Status)
, new SqlParameter("@BAID",ao.BAID)
, new SqlParameter("@PhieuKhamID",ao.PhieuKhamID)
, new SqlParameter("@ThuNganID",ao.ThuNganID)
, new SqlParameter("@Ngay",ao.Ngay)
, new SqlParameter("@SoTien",ao.SoTien)
, new SqlParameter("@LyDo",ao.LyDo)
, new SqlParameter("@GhiChu",ao.GhiChu)
, new SqlParameter("@CreatedBy",ao.CreatedBy)
, new SqlParameter("@CreatedTime",ao.CreatedTime)
, new SqlParameter("@LastModifiedBy",ao.LastModifiedBy)
, new SqlParameter("@LastModifiedTime",ao.LastModifiedTime)
};
int result = int.Parse(SqlHelper.ExecuteScalar(HYPO.Utils.Config.ConnString, CommandType.StoredProcedure, "SP_Bill_Insert", Params).ToString());
return result;
}
catch (Exception ex)
{
if (ex.Message.Contains("duplicate"))
{
return -2;
}
return -1;
}
}
答案 3 :(得分:0)
您需要使用SqlDataAdapter来获取数据表中存储过程的结果。
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
dataGridView1.DataSource = dt;
答案 4 :(得分:0)
您不需要CommandReader
,所有您需要做的就是使用DataAdapter
和DataSet
。并将数据集绑定到DataGridView
DataSet ds = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
答案 5 :(得分:0)
您可以使用DataTable或DataSet来填充数据....这里我使用DataTable ....
Datatable data = new Datatable();
using (SqlDataAdapter adp = new SqlDataAdapter())
{
adp.SelectCommand = cmd;
adp.Fill(data);
GridView1.DataSorce = data;
GridView1.DataBind(); <--- Needed to bind GridView at a time While Filling DataTable data
}
您还可以通过这种方式检查DataTable是否包含数据在将DataTable分配给Gridview1之前.......
if(data.Rows.Counnt > 0)
{
GridView1.DataSorce = data;
GridView1.DataBind();
}
答案 6 :(得分:0)
public void whateverToolStripMenuItem_Click(object sender, EventArgs e) {
// A previously declared and instantiated OpenFileDialog, i put it from Design Mode, but you can just
// declare it as
OpenFileDialog dlgImport = new OpenFileDialog();
//We show the dialog:
dlgImport.ShowDialog();
// We declare a variable to store the file path and name:
string fileName = dlgImport.FileName;
try {
// We invoke our method, wich is created in the following section, and pass it two parameters
// The file name and .... a DataGridView name that we put is the Form, so we can also see what
// We imported. Cool, isn't it?
importExcel(fileName, gridMain);
}
// It is best to always try to handle errors, you will se later why it is OleDbException and not
catch (OleDbException ex) {
MessageBox.Show("Error ocurred: " + ex.Message);
}
}