2017-08-03 27 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

謝謝!這有助於解決問題。我很感激。 –

相關問題