更改列的值
Changing value of column
我想做一个更新,如果我调用填充网格的搜索方法并在其第二列和第三列的反转值后调用更新方法并在 SQL 服务器上更新。
例如:
单击“搜索”按钮填充网格:
约翰 2 3
雪 4 5
白色 6 7
点击更新按钮并再次点击搜索按钮
约翰 3 2
雪 5 4
白 7 6
public void Update(int param_idficha, int param_rgp, int param_quant, double param_peso, string param_date, string param_peixe)
{
vsql = "UPDATE cadastro SET PESO = @PESO, QUANTIDADE = @QUANTIDADE WHERE ID_FICHA = @ID_FICHA and PEIXE LIKE @PEIXE";
SqlCommand objcmd = null;
if (this.Conectar())
{
try
{
DateTime dtParam = DateTime.Parse(param_date);
objcmd = new SqlCommand(vsql, objCon);
objcmd.Parameters.Add(new SqlParameter("@ID_FICHA", param_idficha));
objcmd.Parameters.Add(new SqlParameter("@RGP", param_rgp));
objcmd.Parameters.Add(new SqlParameter("@PEIXE", param_peixe));
objcmd.Parameters.Add(new SqlParameter("@PESO", param_peso));
objcmd.Parameters.Add(new SqlParameter("@QUANTIDADE", param_quant));
objcmd.Parameters.Add(new SqlParameter("@DATA_REGISTRO", dtParam));
objcmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
this.Desconectar();
}
}
}
搜索和填充网格方法:
private void btn_Search_Click(object sender, EventArgs e)
{
if (txtb_idFicha.Text.Length > 0)
{
int i = Convert.ToInt32(txtb_idFicha.Text);
_peixe_list.Clear();
_peso_list.Clear();
_quant_list.Clear();
objSQL.Search_IDFicha(i, _peixe_list, _quant_list, _peso_list);
dataGridView1.Rows.Clear();
for (int j = 0; j < _peixe_list.Count; j++)
{
string[] rows = { _peixe_list[j], _quant_list[j], _peso_list[j] };
dataGridView1.Rows.Add(rows);
}
}
else
MessageBox.Show("Preencha o campo N° Ficha", " ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
更新方法:
private void btn_Update_Click(object sender, EventArgs e)
{
int param_id = Convert.ToInt32(txtb_idFicha.Text);
int param_rgp = Convert.ToInt32(txtb_RGP.Text);
string DateTimesql = myDateTime.ToString("dd-MM-yyyy");
_peixe_list.Clear();
_quant_list.Clear();
_peso_list.Clear();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells.Count >= 2 && row.Cells[0].Value != null)
{
_peixe_list.Add(row.Cells[0].Value.ToString());
_peso_list.Add(row.Cells[1].Value.ToString());
_quant_list.Add(row.Cells[2].Value.ToString());
}
}
for (int x = 0; x < _peixe_list.Count; x++)
objSQL.Update(param_id, param_rgp, Convert.ToInt32(_quant_list[x]), Convert.ToDouble(_peso_list[x]), DateTimesql, _peixe_list[x]);
}
如果我理解正确,您可以按照以下步骤完成任务。
我假设 搜索按钮填充网格 运行 没问题。比更新
而不是
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells.Count >= 2 && row.Cells[0].Value != null)
{
_peixe_list.Add(row.Cells[0].Value.ToString());
_peso_list.Add(row.Cells[1].Value.ToString());
_quant_list.Add(row.Cells[2].Value.ToString());
}
}
您可以做与您在搜索中所做的相同的事情,但只需稍作改动
objSQL.Search_IDFicha(i, _peixe_list, _quant_list, _peso_list);
dataGridView1.Rows.Clear();
for (int j = 0; j < _peixe_list.Count; j++)
{
string[] rows = { _peixe_list[j], _peso_list[j], _quant_list[j] };
dataGridView1.Rows.Add(rows);
}
这里我们重新排序了第二列和第三列,为了保存,我们将再次按照您所做的相同更新程序进行
for (int x = 0; x < _peixe_list.Count; x++)
objSQL.Update(param_id, param_rgp, Convert.ToDouble(_peso_list[x]), Convert.ToInt32(_quant_list[x]), DateTimesql, _peixe_list[x]);
我想做一个更新,如果我调用填充网格的搜索方法并在其第二列和第三列的反转值后调用更新方法并在 SQL 服务器上更新。
例如:
单击“搜索”按钮填充网格:
约翰 2 3
雪 4 5
白色 6 7
点击更新按钮并再次点击搜索按钮
约翰 3 2
雪 5 4
白 7 6
public void Update(int param_idficha, int param_rgp, int param_quant, double param_peso, string param_date, string param_peixe)
{
vsql = "UPDATE cadastro SET PESO = @PESO, QUANTIDADE = @QUANTIDADE WHERE ID_FICHA = @ID_FICHA and PEIXE LIKE @PEIXE";
SqlCommand objcmd = null;
if (this.Conectar())
{
try
{
DateTime dtParam = DateTime.Parse(param_date);
objcmd = new SqlCommand(vsql, objCon);
objcmd.Parameters.Add(new SqlParameter("@ID_FICHA", param_idficha));
objcmd.Parameters.Add(new SqlParameter("@RGP", param_rgp));
objcmd.Parameters.Add(new SqlParameter("@PEIXE", param_peixe));
objcmd.Parameters.Add(new SqlParameter("@PESO", param_peso));
objcmd.Parameters.Add(new SqlParameter("@QUANTIDADE", param_quant));
objcmd.Parameters.Add(new SqlParameter("@DATA_REGISTRO", dtParam));
objcmd.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
this.Desconectar();
}
}
}
搜索和填充网格方法:
private void btn_Search_Click(object sender, EventArgs e)
{
if (txtb_idFicha.Text.Length > 0)
{
int i = Convert.ToInt32(txtb_idFicha.Text);
_peixe_list.Clear();
_peso_list.Clear();
_quant_list.Clear();
objSQL.Search_IDFicha(i, _peixe_list, _quant_list, _peso_list);
dataGridView1.Rows.Clear();
for (int j = 0; j < _peixe_list.Count; j++)
{
string[] rows = { _peixe_list[j], _quant_list[j], _peso_list[j] };
dataGridView1.Rows.Add(rows);
}
}
else
MessageBox.Show("Preencha o campo N° Ficha", " ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
更新方法:
private void btn_Update_Click(object sender, EventArgs e)
{
int param_id = Convert.ToInt32(txtb_idFicha.Text);
int param_rgp = Convert.ToInt32(txtb_RGP.Text);
string DateTimesql = myDateTime.ToString("dd-MM-yyyy");
_peixe_list.Clear();
_quant_list.Clear();
_peso_list.Clear();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells.Count >= 2 && row.Cells[0].Value != null)
{
_peixe_list.Add(row.Cells[0].Value.ToString());
_peso_list.Add(row.Cells[1].Value.ToString());
_quant_list.Add(row.Cells[2].Value.ToString());
}
}
for (int x = 0; x < _peixe_list.Count; x++)
objSQL.Update(param_id, param_rgp, Convert.ToInt32(_quant_list[x]), Convert.ToDouble(_peso_list[x]), DateTimesql, _peixe_list[x]);
}
如果我理解正确,您可以按照以下步骤完成任务。
我假设 搜索按钮填充网格 运行 没问题。比更新
而不是
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells.Count >= 2 && row.Cells[0].Value != null)
{
_peixe_list.Add(row.Cells[0].Value.ToString());
_peso_list.Add(row.Cells[1].Value.ToString());
_quant_list.Add(row.Cells[2].Value.ToString());
}
}
您可以做与您在搜索中所做的相同的事情,但只需稍作改动
objSQL.Search_IDFicha(i, _peixe_list, _quant_list, _peso_list);
dataGridView1.Rows.Clear();
for (int j = 0; j < _peixe_list.Count; j++)
{
string[] rows = { _peixe_list[j], _peso_list[j], _quant_list[j] };
dataGridView1.Rows.Add(rows);
}
这里我们重新排序了第二列和第三列,为了保存,我们将再次按照您所做的相同更新程序进行
for (int x = 0; x < _peixe_list.Count; x++)
objSQL.Update(param_id, param_rgp, Convert.ToDouble(_peso_list[x]), Convert.ToInt32(_quant_list[x]), DateTimesql, _peixe_list[x]);