使用文本框更改datagridview数据
本文关键字:datagridview 数据 文本 | 更新日期: 2023-09-27 18:13:10
我有一个datagridview,我想通过在文本框中键入员工编号来编辑它。然后,它将仅为该员工号更新datagridview。这是我的代码为我当前的datagridview,从数据库中的所有员工提取信息。我需要能够更改选择语句,以便它从文本框中提取出纳员号码并将其插入sql语句。
using (OleDbConnection con = new OleDbConnection(constring))
{
try
{
con.Open();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
//Build DataGridView
try
{
sqlAdapter = new OleDbDataAdapter("SELECT TellerNum, SessionName, PrincipleName, SessionDate, Comments, SessionKey FROM [SESSION] ORDER BY TellerNum;", con);
sqlCommand = new OleDbCommandBuilder(sqlAdapter);
dataset = new DataSet();
sqlAdapter.Fill(dataset, "[Session]");
dataGridView1.DataSource = null;
dataGridView1.DataSource = dataset.Tables["[Session]"];
/*DataTable table1 = GetTable1Data(...);
DataTable table2 = GetTable2Data(...);
table1.Merge(table2, true);*/
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewLinkCell linkCell = new DataGridViewLinkCell();
dataGridView1[5, i] = linkCell;
}
//Change column names
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
if (column.HeaderText == "TellerNum")
column.HeaderText = "Teller #";
if (column.HeaderText == "SessionName")
column.HeaderText = "Session";
if (column.HeaderText == "PrincipleName")
column.HeaderText = "Principle Taught";
if (column.HeaderText == "SessionDate")
column.HeaderText = "Date of Session";
if (column.HeaderText == "Comments")
column.HeaderText = "Comments About Session";
if (column.HeaderText == "SessionKey")
column.HeaderText = " ";
}
dataGridView1.Columns[5].Visible = false;
// Resize the DataGridView columns to fit the newly loaded content.
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
在过去,我已经能够从这样的文本框中提取信息:
string cmdstring = "UPDATE [EMPLOYEE] SET [Comments] = Comments + @comments WHERE [TellerNum] = @teller";
using (OleDbCommand cmd = new OleDbCommand(cmdstring, con))
{
cmd.Parameters.AddWithValue("@teller", comboBox14.Text);
cmd.Parameters.AddWithValue("@comments", textBox5.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Submitted Successfully");
是否有一种方法可以从文本框中提取信息以确定将显示哪些员工信息?
您需要做的是在网格上方有一个用于过滤的组合框。让过滤器组合框填充cbxFilter_SelectedIndexChanged()上的gridView(根据数据库中的某些相关字段查询数据库)。
获得所需的过滤信息后,可以使用文本框编辑数据库中@选定行的信息。您提供的代码将与此文本框相关。
这是一个搜索栏的示例。我为事物的命名惯例道歉。你可以把它作为参考,这才是最重要的。
private void txtSearch_EditValueChanged(object sender, EventArgs e)
{
try
{
sqlHelper.sqlconnection.Close();
}
catch (Exception)
{
return;
}
try
{
sqlHelper.sqlconnection.Open();
string search = txtSearch.Text;
string temp;
temp = sqlHelper.ReplaceValue(cbxWarehouse.Text, true);
string tableName = "dbo.db_" + temp;
SqlDataReader myReader = null;
SqlCommand sql = new SqlCommand("SELECT * FROM " + tableName + " WHERE (StoreNo LIKE'" + search + "') OR " +
"(Description LIKE '%" + search + "%') OR " +
"(Name LIKE '%" + search + "%') OR " +
"(ItemNo LIKE '%" + search + "%') OR " +
"(StoreName LIKE '%" + search + "') AND Assembly <> 1", sqlHelper.sqlconnection);
myReader = sql.ExecuteReader();
DataTable dt = new DataTable();
for (int i = 0; i < myReader.FieldCount; i++)
{
dt.Columns.Add(myReader.GetName(i), typeof(string));
}
try
{
while (myReader.Read())
{
DataRow row = dt.Rows.Add();
for (int i = 0; i < myReader.FieldCount; i++)
{
string Val = myReader[i].ToString();
row[i] = Val;
}
}
myReader.Close();
}
finally
{
gridProducts.DataSource = dt;
}
}
catch
{
if (myReader != null)
{
if (!myReader.IsClosed)
{
myReader.Close();
}
}
}
}
您可以使用您拥有的代码通过文本框进行更新