我如何填写Datagridview并在按钮上发送其数据点击 [英] How Could I Achive To Fill Datagridview And Send Its Data On Button Click
问题描述
我需要将数据从datagridview保存到表,因为表包含更多列,但我在datagridview中只显示了几列。通过sendkeys事件从另一个设备发送文本来填充dgv。我只想要当我点击发送数据按钮时,它将获得dgv的所有textfileds值并存储到数据库表,并且dgv需要清除(因为我将数据从此表移动到另一个表后删除它包含)。目前我从单元格获取文本,但在调用RowLeave事件后,我想在发送数据按钮上获取文本。
如何实现这一目标?请帮忙。
I need to save data from datagridview to table , as table contains more columns but i showed only few columns in datagridview. The dgv fill by sending text from another device with sendkeys event . I just want that when i click "send data" button , it will get all textfileds values of dgv and store to database table and dgv needs to be clear(as i delete its contains after moving data from this table to another table). currently i get text from cells but after calling RowLeave event , i want to get text on "send data" button .
how to achive this? please help.
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)//
{
var senderGrid = (DataGridView)sender;
if (senderGrid.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn && e.RowIndex >= 0)
{
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
string item = row.Cells["item_code"].Value.ToString();
string batch = row.Cells["batchno"].Value.ToString();
string expiry = row.Cells["expiry_date"].Value.ToString();
string reqq = row.Cells["Req_quantity"].Value.ToString();
string trsq = row.Cells["Trs_quantity"].Value.ToString();
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Proc_insertTransferedProduct";
cmd.Parameters.Add(new SqlParameter("itemcode", item));
cmd.Parameters.Add(new SqlParameter("batch", batch));
cmd.Parameters.Add(new SqlParameter("expiry", Convert.ToDateTime(expiry)));
cmd.Parameters.Add(new SqlParameter("req_quantity", reqq));
cmd.Parameters.Add(new SqlParameter("trs_quantity", trsq));
cmd.Connection = con;
table = new DataTable();
adapter = new SqlDataAdapter(cmd);
adapter.Fill(table);
MessageBox.Show("data transfered to table");
}
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//need to do above task here //
}
推荐答案
Dim constr As String = ConfigurationManager.ConnectionStrings(......)ConnectionString
使用con作为新的SqlConnection(constr)
使用cmd作为新的SqlCommand(从表中选择)
使用sda作为新的SqlDataAdapter()
cmd.Connection = con >
sda.SelectCommand = cmd
使用dt作为新数据表()
sda.Fill(dt)
返回dt
结束使用
结束使用
结束使用
结束使用
Dim constr As String = ConfigurationManager.ConnectionStrings("......")ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("select from table")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
这篇关于我如何填写Datagridview并在按钮上发送其数据点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!