我想在通过DataGrid外的一个按钮来向DataGrid里增加一个空行,也就是实现增加功能,空行里我都设置好全是文本框。帮忙!!!!!!!!!

解决方案 »

  1.   

    楼上思路正确,不要试图在 DataGrid里增加一行,在数据源中想办法.
      

  2.   

    This is a sample:<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 47px; POSITION: absolute; TOP: 51px" runat="server"
    AutoGenerateColumns="False" ShowFooter="True" Font-Size="10pt" AllowPaging="True" PageSize="20">
    <EditItemStyle ForeColor="White" BackColor="RoyalBlue"></EditItemStyle>
    <AlternatingItemStyle BackColor="#FFFFC0"></AlternatingItemStyle>
    <ItemStyle BackColor="Bisque"></ItemStyle>
    <HeaderStyle ForeColor="White" BackColor="#C04000"></HeaderStyle>
    <FooterStyle BackColor="Olive"></FooterStyle>
    <Columns>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
    <asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
    <asp:TemplateColumn HeaderText="ID">
    <ItemTemplate>
    <asp:Label id="Label4" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.id") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:LinkButton id="LinkButton1" runat="server" CommandName="Insert">Insert</asp:LinkButton>
    </FooterTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="title">
    <ItemTemplate>
    <asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.title") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox id="Textbox3" runat="server" Width="200"></asp:TextBox>
    </FooterTemplate>
    <EditItemTemplate>
    <asp:TextBox id=TextBox5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.title") %>' Width="200">
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="url">
    <ItemTemplate>
    <asp:Label id="Label2" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.url") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <INPUT type="file" runat="server" id="file1">
    </FooterTemplate>
    <EditItemTemplate>
    <asp:TextBox id="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.url") %>'>
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    <PagerStyle BorderWidth="1px" BorderColor="#FFC080" BorderStyle="Solid"></PagerStyle>
    </asp:DataGrid>=============private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (e.CommandName == "Insert")
    {
    ...
    }
    }
      

  3.   

    使用 OnItemCreated 方法提供 ItemCreated 事件的自定义处理程序。当创建 DataGrid 控件中的项时(不论是在往返行程中还是在将数据绑定到控件时),都会引发 ItemCreated 事件。ItemCreated 事件通常用于控制 DataGrid 控件中行的内容和外观。void Item_Created(Object sender, DataGridItemEventArgs e) 
          {
     
             // Customize the footer section with an image.
             if(e.Item.ItemType == ListItemType.Footer)
             {         
     
               // Create an Image control.
               Image NewImageControl = new Image();           // Set the properties of the Image control.
               NewImageControl.ImageUrl = "Image1.jpg"; 
               NewImageControl.AlternateText = "Image 1";           // Add the Image control to the Controls collection of the 
               // cell representing the third column in the DataGrid.
               e.Item.Cells[2].Controls.Add(NewImageControl);         }
      

  4.   

    在数据源添加一行,代码如下:
    private void BindGrid()
    {
    SqlConnection myConnection = new SqlConnection(Application["DSN"].ToString());
    SqlDataAdapter myCommand = new SqlDataAdapter("SELECT Id, Week_Id, str1, str2, Ocu_Date, Price, str4 FROM abc", myConnection);
    DataSet ds = new DataSet();
    try
    {
    myConnection.Open();
    myCommand.Fill(ds, "att"); DataRow dr;
    dr=ds.Tables["att"].NewRow();
    dr["str1"]="";
    dr["str2"]="";
    dr["Ocu_Date"]=DBNull.Value;
    dr["Price"]=DBNull.Value;
    dr["str4"]="";
    dr["id"]=0; ds.Tables["att"].Rows.Add(dr); if(ds.Tables[0].Rows.Count==0)
    {
    LinkButton14.Text = "显示";
    } dg.DataSource=ds;
    dg.DataBind();
    }
    catch(SqlException e)
    {
    Msg.ForeColor = Color.Red;
    Msg.Text = e.Message;
    return;
    }
    finally
    {
    myConnection.Close();
    }  
      

  5.   

    请将数据源增加一行,重新绑定即可
    =================================
    right!