2011-02-10 97 views
1

我有一個columncontentclicked事件處理程序,它工作正常,直到我用存儲過程縮短datalist。之後,返回的indexnum是0而不是5或6. 是否必須刷新datagridview或其他東西?C#datagridview錯誤的列索引

這裏的TE代碼

int lastcol = dataGridView1.Columns.Count; 
     // MessageBox.Show(e.ColumnIndex.ToString() + lastcol.ToString()); 
     if (e.ColumnIndex == lastcol - 1) 
     { 
      int index = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()); 


      Global.size = this.Size; 
      Global.position = this.Location; 
      Global.overzicht_select = index.ToString(); 


      if (Global.give_return == false) 
      { 

       switch(type) 
       { 

        case 1: 
        Global.edit_form_proj = false; 
        project_form project_form1 = new project_form(this); 
        project_form1.Show(this); 
        this.Hide(); 
        break; 



        case 2: 
        Global.edit_form_bedr = false; 
        bedrijf_form bedrijf_form1 = new bedrijf_form(this); 
        bedrijf_form1.Show(this); 
        this.Hide(); 
        break; 


        case 3: 
        Global.edit_form_pers = false; 
        persoon_form persoon_form1 = new persoon_form(this); 
        persoon_form1.Show(this); 
        this.Hide(); 
        break; 
       } 
      } 
      else 
      { 
       Global.return_id = index.ToString(); 
       if (pf != null) 
       { 
        pf.fill_id(); 
       } 
       if (pr != null) 
       { 
        pr.fill_id(); 
       } 
       Global.give_return = false; 
       Close(); 
      } 
     } 
    } 

}

+0

你試過調用`dataGridView1.DataBind()`嗎? – 2011-02-10 11:29:30

回答

0

,我發現我的問題。我想要點擊的列是一個按鈕列,在加載網格時添加。但過濾後,該列不會得到刷新或獲得新的數據,所以它是我第一個也是唯一的列,而其他列重建。所以調用dataGridView1.Columns.Clear();並在網格重新填充後重新創建按鈕列做了訣竅, -