如何在此命令后激活更新或在datagridview1中插入行

时间:2016-01-05 18:09:24

标签: c#

 //insert one record with this code
sqlcon.Open();
string query1 = "Sp_InsertUpdateStudent";
SqlCommand cmnd1 = new SqlCommand(query1, sqlcon);
cmnd1.CommandType = CommandType.StoredProcedure;
cmnd1.Parameters.AddWithValue("@Id", txtid.Text.ToString());
cmnd1.Parameters.AddWithValue("@studentcode", txtcode.Text.ToString());
cmnd1.Parameters.AddWithValue("@firstname", txtfirstname.Text.ToString());
cmnd1.Parameters.AddWithValue("@lastname", txtlastname.Text.ToString());
cmnd1.ExecuteNonQuery();
MessageBox.Show(" complete ");}

//在这个地方我怎样才能在datagridview1中选择插入行

1 个答案:

答案 0 :(得分:0)

首先,您需要将列添加到dataGridView,如下所示:

dataGridView1.Columns.Add("Id", "Id");
dataGridView1.Columns.Add("studentcode", "studentcode");
dataGridView1.Columns.Add("firstname", "firstname");
dataGridView1.Columns.Add("lastname", "lastname");

然后您可以将数据传递给dataGridView

object[] rowData = new object[dataGridView1.Columns.Count];

rowData[0] = txtid.Text;     
rowData[1] = txtcode.Text;        
rowData[2] = txtfirstname.Text;        
rowData[3] = txtlastname.Text;     
dataGridView1.Rows.Add(rowData);