在GridView中添加空行时出错 [英] Error in adding empty row in gridview

查看:65
本文介绍了在GridView中添加空行时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面是在Gridview中添加空行的代码

Hi,

Below is code to add a empty row in Gridview

protected void PrintGv_PreRender(object sender, EventArgs e)
    {
       
        
       GridViewRow gv = new GridViewRow(1,-1,DataControlRowType.Header,DataControlRowState.Normal);
           
            
        TableCell tbCell = new TableCell();
        tbCell.ColumnSpan = 3;
        tbCell.Text = "GridView Header";
        tbCell.Attributes.Add("style", "text-align:center");
        gv.Cells.Add(tbCell);

        PrintGv.Controls[0].Controls.AddAt(1, gv);

}



但我收到此错误
指定的参数超出有效值范围.
参数名称:index

需要您的帮助来解决此问题.

谢谢



But i am getting this error
Specified argument was out of the range of valid values.
Parameter name: index

Need your help to solve this issue.

Thanks

推荐答案


看到这个例子,它会给你一个主意:),
希望对您有所帮助.
Hi ,
See This Example it will Give you Idea :),
Hope it''s help you .
<div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

           onrowdatabound="GridView1_RowDataBound">
           <Columns>
               <asp:TemplateField HeaderText="item code">
                   <ItemTemplate>
                       <asp:Label ID="lblID" runat="server" Text="<%# Bind('item_code') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Item name">
                   <ItemTemplate>
                       <asp:Label ID="lblItemName" runat="server" Text="<%# Bind('Item_name') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="brand">
                   <ItemTemplate>
                       <asp:Label ID="Label2" runat="server" Text="<%# Bind('brand') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="size">
                   <ItemTemplate>
                       <asp:Label ID="lblSize" runat="server" Text="<%# Bind('size') %>"></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
           </Columns>
       </asp:GridView>
   </div>


背后的代码:


Code Behind:

protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           var con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
           string statment = "select item_code, Item_name, brand, size  from Items ";
           SqlDataAdapter da = new SqlDataAdapter(statment, con);
           DataTable dt = new DataTable();
           da.Fill(dt);
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
   }
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       //Check if it's header
       if (e.Row.RowType == DataControlRowType.Header)
       {
                 //Create new Row in Gridview
         GridViewRow  row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
             //create Cell and it will be added to row
          TableCell   cell = new TableCell();
           cell.Text = "0";
           row.Cells.Add(cell);


           TableCell cell1 = new TableCell();
           cell1.Text = "Item Name Test";
           row.Cells.Add(cell1);


          TableCell cell2 = new TableCell();
           cell2.Text = "eeeee";
           row.Cells.Add(cell2);


         TableCell  cell3 = new TableCell();
           cell3.Text = "ssss";
           row.Cells.Add(cell3);
                //add the row to Gridview and AddAT(Index of where it will be appear
            GridView1.Controls[0].Controls.AddAt(1, row);
       }
   }


如果您仍有问题,请通知我.
最好的问候
米特瓦里(M.Mitwalli)


If you still have problem let me know .
Best Regards
M.Mitwalli


这篇关于在GridView中添加空行时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆