2012-04-26 33 views
1

我的問題是:如何檢查GridView是否包含按鈕字段

我有一個GridView,它包含一個列(按鈕字段)。現在我想在運行時知道我的Grid是否包含Button字段。

foreach (DataColumn col in Table.Columns) 
       { 
        ButtonField btnfield = new ButtonField(); 
        btnfield.ButtonType = ButtonType.Image; 

        if (grid.Columns.Contains(btnfield)) 
        { 
         grid.Columns.RemoveAt(grid.Columns.IndexOf(btnfield)); 
        } 

       } 

此代碼無效。我想在沒有行數據綁定的情況下完成這項任務。

問候Zuhaib

回答

1

如果我能在這裏你的問題是你應該做的事情:

foreach (GridViewRow row in YourGridView.Rows) 
{ 
    //This should get the control in the cell, you could use FindControl too. 
    Control ctrl = row.Cells[columnIndex].Controls[0]; 
    //Check the control type 
    if (ctrl.GetType() == typeof(ButtonField)) 
    { 
    } 
}