本帖最后由 ttigerdna 于 2011-04-12 20:21:26 编辑

解决方案 »

  1.   


    int id = Convert.ToInt32(dataGridView1.SelectedRows[SelectedIndex].Cells["Id"].Value);
      

  2.   

    索引超出范围。必须为非负值并小于集合大小。
    参数名: indexArgumentOutOfRangeException
      

  3.   


    int index = dataGridView1.CurrentCell.RowIndex;
    int id = Convert.ToInt32(dataGridView1.SelectedRows[index].Cells["Id"].Value);
      

  4.   

    这是窗体全部代码
    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();
                }
            }
        }
    }
      

  5.   

    int id = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
                    Form3 fd = new Form3(id);
                    fd.ShowDialog();//获取该行ID