2015-06-26 1 views
0

Am сталкивается с Array из исключения для gridview после заполнения его при событии ButtonClick. Тот же источник работает отлично, когда он запускается из загрузки страницы. Не удалось найти аналогичную запись в stackoverflow. Пожалуйста, порекомендуйте. Благодарю.Обновление GridView от аварийных событий Button Click событий

Код:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Cells[0].Attributes["width"] = "30px"; 
    e.Row.Cells[1].Attributes["width"] = "40px"; -> CRASHES AT INDEX 1 
    e.Row.Cells[2].Attributes["width"] = "40px"; 
    e.Row.Cells[3].Attributes["width"] = "50px"; 
    e.Row.Cells[4].Attributes["width"] = "50px"; 
    e.Row.Cells[5].Attributes["width"] = "100px"; 
    e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left; 
} 

<asp:GridView ID="GridView1" runat="server" CellPadding="4" 
    ForeColor="Black" GridLines="Horizontal" Width="100%" ShowHeader="true" 
    EmptyDataText="No Records found.!" 
    RowStyle-Wrap="true" AllowPaging="true" PageSize="5" 
    OnPageIndexChanging="gridView_PageIndexChanging" 
    OnRowDataBound="GridView1_RowDataBound"   
    style="table-layout:fixed; word-wrap:break-word; margin-left: 0px;" > 

ответ

1

Вы должны проверять, чтобы увидеть, какой тип строки это. Я проверяю DataRow, но вы можете сделать это только для заголовка.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Cells[0].Attributes["width"] = "30px"; 
     e.Row.Cells[1].Attributes["width"] = "40px"; -> CRASHES AT INDEX 1 
     e.Row.Cells[2].Attributes["width"] = "40px"; 
     e.Row.Cells[3].Attributes["width"] = "50px"; 
     e.Row.Cells[4].Attributes["width"] = "50px"; 
     e.Row.Cells[5].Attributes["width"] = "100px"; 
     e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Left; 
    } 
} 
+0

Thanks Rick. Это решило проблему. – Vasanth

Смежные вопросы