2017-08-03 47 views
0

我遇到的问题与我的dataGridView导致单击视图的列标题时出现EventOutOfRange异常错误。我明白为什么发生错误,我只是在创建工作时遇到问题。有没有办法可以创建一个if语句来查看所选索引是否为-1?我所尝试过的一切都是无效的。SelectedIndexChanged EventOutOfRange在C#中单击datagridview标题时出现异常

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; 

     } 
    } 
+0

_if(theGrid!= NULL && theGrid.SelectedRows!= NULL)_ – Steve

+0

两个条件都满足,错误仍然发生。有没有办法做&& theGrid.SelectedRows!= -1)。我这样做的时候会出错。 –

+0

在_if(.. && theGrid.SelectedRows.Count> 0)上添加一个检查_ – Steve

回答

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; } 
    } 
} 
+0

谢谢!这有助于解决问题。我很感激。 –

相关问题