我按照以下方式演示了DGV。 我正在尝试将文本框的输入添加到DGV中,如下所示。
private void Form2_Load(object sender, EventArgs e)
{
DataGridViewColumn srno = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Insert(0, srno);
DataGridViewColumn part = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Insert(0, part);
DataGridViewColumn cts = new DataGridViewTextBoxColumn();
cts.ValueType = typeof(decimal);
dataGridView1.Columns.Insert(0, cts);
DataGridViewColumn rt =new DataGridViewTextBoxColumn();
rt.ValueType = typeof(decimal);
dataGridView1.Columns.Insert(0, rt);
DataGridViewColumn debit =new DataGridViewTextBoxColumn();
debit.ValueType = typeof(decimal);
dataGridView1.Columns.Insert(0, debit);
}
// textBox EventHandler
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Enter)
{
int i = dataGridView1.CurrentCell.RowIndex;
dataGridView1[1, i].Value = textBox1.Text;
dataGridView1.Focus();
}
}
private void Form1_Load(object sender, EventArgs e)
{
string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah";
SqlConnection con = new SqlConnection(connstr);
con.Open();
DataSet mydatasett;
string dgv = " select srno,particulars,carats,rate,debit from depurchaseA";
SqlCommand dgvcmd = new SqlCommand(dgv, con);
SqlDataAdapter dgvdap = new SqlDataAdapter(dgvcmd);
mydatasett = new DataSet();
dgvdap.Fill(mydatasett);
bindingsource2 = new BindingSource();
bindingsource2.DataSource = mydatasett;
bindingsource2.DataMember = mydatasett.Tables[0].TableName;
dataGridView1.DataSource = bindingsource2;
}
**//And textbox Event handler :**
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Enter)
{
int i = dataGridView1.CurrentCell.RowIndex;
dataGridView1[1, i].Value = textBox1.Text;
dataGridView1.Focus();
}
}
以上在UnBound DGV上工作正常但在绑定DGV中不起作用。我想将textBox的输入添加到Bound DGV中。有简单的方法吗?。
答案 0 :(得分:1)
private void btnUpdate_Click(object sender, EventArgs e)
{
// private String connectionString = null;
// private SqlConnection sqlConnection = null;
btnBack.Enabled = true;
sqlConnection.Open();
dataGridView1.DataSource = bindingSource;
//cmd = new SqlCommand("update empinfo set empname=@empname, empAdd=@empAdd, empMobile=@empMobile where empid=@empid", con);
cmd = new SqlCommand("empinfo_Insert_Update_Delete", sqlConnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd1 = new SqlCommand("Insert_Update_Delete_EmpSal", sqlConnection);
cmd1.CommandType = CommandType.StoredProcedure;
try
{
cmd.Parameters.AddWithValue("@empid", txtempId.Text);
cmd.Parameters.AddWithValue("@empName", txtempName.Text);
cmd.Parameters.AddWithValue("@empAdd", txtempAdd.Text);
cmd.Parameters.AddWithValue("@empMobile", TxtempMobile.Text);
cmd.Parameters.AddWithValue("@intflag", 1);
//txtempId.Text = txtsalempId.Text;
cmd1.Parameters.AddWithValue("@salId", txtsalId.Text);
cmd1.Parameters.AddWithValue("@salAmount", txtsalary.Text);
cmd1.Parameters.AddWithValue("@salDate", txtdos.Text);
cmd1.Parameters.AddWithValue("@empId", txtempId.Text);
cmd1.Parameters.AddWithValue("@intflag", 1);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
for (int i = 0; i < dataTable.Rows.Count; i++)
{
dataTable.Rows[i][3] = dataTable.Rows[0][3];
}
sqlDataAdapter.Update(dataTable);
//int b;
//b = int.Parse(txtempId.Text);
//selectQueryString1 = "SELECT * FROM empsal where empid=" + b;
////sqlDataAdapter1 = new SqlDataAdapter(selectQueryString1, sqlConnection);
////sqlCommandBuilder1 = new SqlCommandBuilder(sqlDataAdapter1);
////dataTable1 = new DataTable();
////sqlDataAdapter1.Fill(dataTable1);
////bindingSource1 = new BindingSource();
////bindingSource1.DataSource = dataTable1;
////dataGridView1.DataSource = bindingSource1;
MessageBox.Show("data Updated");
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
cmd1 = null;
dataGridView1.DataSource = null;
sqlConnection.Close();
clearText();
addcolumn();
childform();
}
答案 1 :(得分:0)
已经有一段时间了,但你试过dataGridView.Rows [i] .Cells [1] .Value = textBox1.Text?
我会在你的texbox处理程序中放一个断点并检查dataGridView1.CurrentCell的值,确保它是一个值,并指向你所期望的值。
答案 2 :(得分:0)
有一个简单的解决方案编辑/添加/删除 Datagridview To Evoke行或
暂停特定事件处理程序的temparory源代码,如下所示。
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Enter)
{
bindingsource2.ResumeBinding (); // OR bindingsource2.SuspendBinding();
int i = dataGridView1.CurrentCell.RowIndex;
dataGridView1[1, i].Value = textBox1.Text;
dataGridView1.Focus();
}
}
这里在我的情况下只有ResumeBinding()方法是Works。 SuspendBinding()方法将以不同的方式使用。