DataGridView 代码出错 本帖最后由 ttigerdna 于 2011-04-12 20:21:26 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int id = Convert.ToInt32(dataGridView1.SelectedRows[SelectedIndex].Cells["Id"].Value); 索引超出范围。必须为非负值并小于集合大小。参数名: indexArgumentOutOfRangeException int index = dataGridView1.CurrentCell.RowIndex;int id = Convert.ToInt32(dataGridView1.SelectedRows[index].Cells["Id"].Value); 这是窗体全部代码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 part1{ public partial class Form1 : Form { public DataSet dataSet = new DataSet(); public SqlDataAdapter dataAdapter; public Form1() { InitializeComponent(); } private void btnClose_Click(object sender, EventArgs e) { Application.Exit(); } private void Form1_Load(object sender, EventArgs e) { string sql = "select Id,FlightNO,LeaveCity,Destination,LeaveTime from TicketInfo"; dataAdapter = new SqlDataAdapter(sql, DBHelper.con); dataAdapter.Fill(dataSet, "TicketInfo"); dataGridView1.DataSource = dataSet.Tables["TicketInfo"]; } public bool JianCha() { if (txtChuFa.Text.Trim() == "") { MessageBox.Show("请输入出发地!"); this.txtChuFa.Focus(); return false; } else if (txtMuDi.Text.Trim() == "") { MessageBox.Show("请输入目的地!"); this.txtMuDi.Focus(); return false; } else { return true; } } private void btnChaXun_Click(object sender, EventArgs e) { if (JianCha()==true) { try { string sql = string.Format("select Id,FlightNO,LeaveCity,Destination,LeaveTime from TicketInfo where LeaveCity='{0}' and Destination='{1}'", txtChuFa.Text, txtMuDi.Text); dataAdapter.SelectCommand.CommandText = sql; dataSet.Tables["TicketInfo"].Clear(); dataAdapter.Fill(dataSet, "TicketInfo"); //dataGridView1.DataSource = dataSet.Tables["TicketInfo"]; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { } } } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { //int index = dataGridView1.CurrentCell.RowIndex; //int id = Convert.ToInt32(dataGridView1.SelectedRows[index].Cells["Id"].Value); int index = dataGridView1.CurrentCell.RowIndex; int id = Convert.ToInt32(dataGridView1.SelectedRows[index].Cells["Id"].Value); //int id = Convert.ToInt32(dataGridView1.SelectedRows[SelectedIndex].Cells["Id"].Value); string sql = string.Format("select id,LeaveCity,Destination,LeaveTime,SecondClass,FirstClass from TicketInfo where id={0}", id); string LeaveCity, Destination, LeaveTime, SecondClass, FirstClass; try { DBHelper.con.Open(); SqlCommand comm = new SqlCommand(sql, DBHelper.con); SqlDataReader dataReader = comm.ExecuteReader(); if (!dataReader.HasRows) { MessageBox.Show("没有数据"); } else { dataReader.Read(); LeaveCity = (string)dataReader["LeaveCity"]; Destination = (string)dataReader["Destination"]; LeaveTime = (string)dataReader["LeaveTime"]; SecondClass=Convert.ToString(dataReader["SecondClass"]); FirstClass = Convert.ToString(dataReader["FirstClass"]); txtChuFa2.Text = LeaveCity; txtMuDi2.Text = Destination; txtTime.Text = LeaveTime; txtJingji.Text = SecondClass; txtTouDeng.Text = FirstClass; dataReader.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { DBHelper.con.Close(); } } }} int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value); Form3 fd = new Form3(id); fd.ShowDialog();//获取该行ID 有关FastReport显示? listview选中某项如何不变蓝色背景和字体颜色 C#随机数问题,在线等! VS2008发布网站以后为什么有的代码会有空格? C#获取外网IP 如何访问BindingSource里的数据 一个关于C1FlexGrid奇怪问题 关于DataGrid的问题。 WPF,这个Button点击如何换成MVVM理念整 有介绍word.dll或者office.dll里面内置类的资料吗? C# 交流关于Access连接字符串相对路径连接方法 【求助】如何在文本框中查看网页源码
int id = Convert.ToInt32(dataGridView1.SelectedRows[SelectedIndex].Cells["Id"].Value);
参数名: indexArgumentOutOfRangeException
int index = dataGridView1.CurrentCell.RowIndex;
int id = Convert.ToInt32(dataGridView1.SelectedRows[index].Cells["Id"].Value);
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 part1
{
public partial class Form1 : Form
{
public DataSet dataSet = new DataSet();
public SqlDataAdapter dataAdapter;
public Form1()
{
InitializeComponent();
} private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
} private void Form1_Load(object sender, EventArgs e)
{
string sql = "select Id,FlightNO,LeaveCity,Destination,LeaveTime from TicketInfo";
dataAdapter = new SqlDataAdapter(sql, DBHelper.con);
dataAdapter.Fill(dataSet, "TicketInfo");
dataGridView1.DataSource = dataSet.Tables["TicketInfo"];
} public bool JianCha()
{
if (txtChuFa.Text.Trim() == "")
{
MessageBox.Show("请输入出发地!");
this.txtChuFa.Focus();
return false;
}
else if (txtMuDi.Text.Trim() == "")
{
MessageBox.Show("请输入目的地!");
this.txtMuDi.Focus();
return false;
}
else
{
return true;
}
}
private void btnChaXun_Click(object sender, EventArgs e)
{
if (JianCha()==true)
{
try
{
string sql = string.Format("select Id,FlightNO,LeaveCity,Destination,LeaveTime from TicketInfo where LeaveCity='{0}' and Destination='{1}'", txtChuFa.Text, txtMuDi.Text);
dataAdapter.SelectCommand.CommandText = sql;
dataSet.Tables["TicketInfo"].Clear();
dataAdapter.Fill(dataSet, "TicketInfo");
//dataGridView1.DataSource = dataSet.Tables["TicketInfo"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
}
}
} private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ } private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
//int index = dataGridView1.CurrentCell.RowIndex;
//int id = Convert.ToInt32(dataGridView1.SelectedRows[index].Cells["Id"].Value); int index = dataGridView1.CurrentCell.RowIndex;
int id = Convert.ToInt32(dataGridView1.SelectedRows[index].Cells["Id"].Value); //int id = Convert.ToInt32(dataGridView1.SelectedRows[SelectedIndex].Cells["Id"].Value);
string sql = string.Format("select id,LeaveCity,Destination,LeaveTime,SecondClass,FirstClass from TicketInfo where id={0}", id);
string LeaveCity, Destination, LeaveTime, SecondClass, FirstClass;
try
{
DBHelper.con.Open();
SqlCommand comm = new SqlCommand(sql, DBHelper.con);
SqlDataReader dataReader = comm.ExecuteReader(); if (!dataReader.HasRows)
{
MessageBox.Show("没有数据");
}
else
{
dataReader.Read(); LeaveCity = (string)dataReader["LeaveCity"];
Destination = (string)dataReader["Destination"];
LeaveTime = (string)dataReader["LeaveTime"];
SecondClass=Convert.ToString(dataReader["SecondClass"]);
FirstClass = Convert.ToString(dataReader["FirstClass"]); txtChuFa2.Text = LeaveCity;
txtMuDi2.Text = Destination;
txtTime.Text = LeaveTime;
txtJingji.Text = SecondClass;
txtTouDeng.Text = FirstClass;
dataReader.Close();
} }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
DBHelper.con.Close();
}
}
}
}
Form3 fd = new Form3(id);
fd.ShowDialog();//获取该行ID