读书人

高手都进来出现有关问题了。dataGri

发布时间: 2012-06-25 18:37:39 作者: rapoo

高手都进来,出现问题了。dataGridView1填充数据出现的问题。
dataGridView1填充数据出现的问题。我想在dataGridView1中的每行的第3列输入编号,按enter键,键检索数据数,有该编号的话,就填充该行数据,然后跳到第5列,数了数量再跳到下一行的第3列继续输入,这样循环,前面两行还正确,第三行开始不正确了。大家帮我看看。我把代码贴出来。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 数据库测试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}



SqlConnection conn;

private DataTable ScannedDt = new DataTable();

private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Columns[1].ReadOnly = true;
dataGridView1.Columns[4].ReadOnly = false;
dataGridView1.Columns[0].ReadOnly = true;
dataGridView1.Columns[2].ReadOnly = false;
dataGridView1.Columns[3].ReadOnly = false;
dataGridView1.Columns[5].ReadOnly = true;
dataGridView1.Columns[6].ReadOnly = true;
dataGridView1.Columns[7].ReadOnly = true;


conn = new SqlConnection("server=.;database=SJZL;integrated security=true;");
SqlDataAdapter sda = new SqlDataAdapter("select * from SJZL_ERP_BCPCK", conn);
DataTable Dt = new DataTable();
sda.Fill(Dt);
ScannedDt = Dt.Clone();
this.dataGridView1.AutoGenerateColumns = false;
}
protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
{

if (e.KeyCode == System.Windows.Forms.Keys.Enter)
{

e.Handled = true;

}
}
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
switch (keyData)
{
case System.Windows.Forms.Keys.Enter:

if (dataGridView1.CurrentCell == dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2] )
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4];


string spbm = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString().Trim();
AddCode(spbm, dataGridView1);

}
else if (dataGridView1.CurrentCell.ColumnIndex == 4)
{


dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex+1].Cells[2];

}


return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}


public DataTable rows()
{
DataSet _ds = new DataSet();
SqlConnection conn = new SqlConnection("server=.;database=SJZL;integrated security=true;");

SqlDataAdapter _da = new SqlDataAdapter("select * SJZL_ERP_BCPCK where bcprkid='" + dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString().Trim() + "' ", conn);
_da.Fill(_ds);


return _ds.Tables[0];
}


private void AddCode(string Code, DataGridView Dgv)
{

Boolean Exist = false;

conn.Open();
Exist = (int)new SqlCommand("select count(*) from SJZL_ERP_BCPCK where bcprkid='" + dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString().Trim() + "'", conn).ExecuteScalar() > 0;
conn.Close();

if (Exist)
{

SqlDataAdapter sda = new SqlDataAdapter("select * from SJZL_ERP_BCPCK where bcprkid='" + dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString().Trim() + "' ", conn);

DataTable Dt = new DataTable();
sda.Fill(Dt);

ScannedDt.ImportRow(Dt.Rows[0]);
dataGridView1.DataSource = ScannedDt;
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4];



}
else
{
MessageBox.Show("不存在此条形码!");
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex ].Cells[2];
}

}





}
}


我QQ376529406,有想远程帮助我的可以加我,麻烦。

[解决办法]
给 dataGridView1.DataSource 赋值后 ScannedDt 行数奇异增多了

读书人网 >C#

热点推荐