SelectedIndexChanged EventOutOfRange在C#中单击datagridview头时出现异常

时间:2017-08-03 20:27:52

标签: c# winforms datagridview

我的dataGridView出现问题,导致在单击视图的列标题时出现EventOutOfRange异常错误。我理解为什么会发生错误,我只是在创建一个解决方案时遇到问题。有没有办法可以创建一个if语句来查看所选索引是否为-1?我尝试的一切都是无效的。

    private void student_grid_SelectionChanged(object sender, EventArgs e)
    {
        DataGridView theGrid = sender as DataGridView;
        if (theGrid != null)
        {
            //The line below is what causes the error.
            DataGridViewRow row = theGrid.SelectedRows[0];
            stuinfo_fill.Text = row.Cells["Student ID"].Value.ToString();    
        }
        if (stuinfo_fill.Text == String.Empty)
        {
            MessageBox.Show("You must have a student selected.");
        }
        else
        {
            PhysicianFill(stuinfo_fill.Text);
            StudentInfo(stuinfo_fill.Text);
            sch_rad.Checked = false;
            newsch_btn.Enabled = true;
            schList_btn.Enabled = true;
            phynew_btn.Enabled = true;
            phylist_btn.Enabled = true;

        }
    }

1 个答案:

答案 0 :(得分:0)

希望这可以帮助

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace DatagridView_45493968
{
    public partial class Form1 : Form
    {
        BindingList<dgventry> dgvItemList = new BindingList<dgventry>();
        DataGridView dgv = new DataGridView();
        TextBox txtb = new TextBox();
        public Form1()
        {
            InitializeComponent();
            //Nothing in the wysiwyg form, everything in codeBehind
            MakeTheGrid();
            MakeTheTextBox();
            /**/

            //add some records to the Grid
            dgvItemList.Add(new dgventry { col1 = "1", col2 = "11", col3 = "111" });
            dgvItemList.Add(new dgventry { col1 = "2", col2 = "22", col3 = "222" });
            dgvItemList.Add(new dgventry { col1 = "3", col2 = "22", col3 = "333" });


        }

        private void Dgv_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView theGrid = sender as DataGridView;

            //if the grid aint null, and the grid has rows, and something is selected
            if (theGrid != null && theGrid.RowCount > 0 && theGrid.SelectedCells.Count > 0)
            {
                //get the `OwningRow` of the selected Cells and do your thing!
                txtb.Text = (string)theGrid.SelectedCells[0].OwningRow.Cells[0].FormattedValue;
            }
        }

        private void MakeTheTextBox()
        {
            txtb.Location = new Point(dgv.Location.X, dgv.Location.Y + dgv.Height + 5);
            this.Controls.Add(txtb);
        }

        private void MakeTheGrid()
        {
            dgv.Location = new Point(this.Location.X + 5, this.Location.Y + 5);
            dgv.DataSource = dgvItemList;
            this.Controls.Add(dgv);
            dgv.SelectionChanged += Dgv_SelectionChanged;
        }
    }

    public class dgventry
    {
        public string col1 { get; set; }
        public string col2 { get; set; }
        public string col3 { get; set; }
    }
}