日期:2014-05-18  浏览次数:20824 次

高手都进来,出现问题了。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