在选择组合框中的项目时,从数据库中填充数据视图
本文关键字:数据库 填充 视图 数据 项目 组合 选择 | 更新日期: 2023-09-27 18:13:12
我有一个包含申请号的组合框。现在,当我在组合框中选择1个请求项(如r001)时,它的信息应该填充在下面的数据网格中。为此我使用了一个stroed过程。但是我不知道如何将datagrid视图控件绑定到info.
代码:private void cmbreqno_SelectedIndexChanged(object sender, EventArgs e)
{
cmd.Connection = con;
if (con.State != ConnectionState.Open)
{
con.Open();
}
txtcc.Text = "";
int selection = Convert.ToInt16(((KeyValuePair<string, string>)(cmbreqno.SelectedItem)).Key);
if (selection.ToString() != "")
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "itemname";
cmd.Parameters.AddWithValue("@req_no", selection);
dsitemname.Clear();
adp = new SqlDataAdapter(cmd);
adp.Fill(dsitemname);
txtcc.Text = dsitemname.Tables[1].Rows[0]["costcenter_no"].ToString();
txtcc.Tag = dsitemname.Tables[1].Rows[0]["costcenter_id"].ToString();
cmd.Parameters.Clear();
}
存储过程:alter proc itemname
(
@req_no int
)
as begin
select item_name,brand_name,quantity,requisitionitem.item_cost
from requisitionitem left outer join item
on requisitionitem.item_id=item.item_id
where requisitionitem.req_no=@req_no
end
将您的DataTable
绑定到DataGridView
dataGridView1.DataSource = null; //clear old one
dataGridView1.Rows.Clear(); //remove old rows
dataGridView1.DataSource = dsitemname.Tables[0];
应该如下所示:
con.Open();
int selection = Convert.ToInt16(((KeyValuePair<string, string>)(cmbreqno.SelectedItem)).Key);
cmd = new SqlCommand("storedProcedureName", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@req_no",selection);
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
cmd.ExecuteNonQuery();