当Table source为空以输入新数据时,在GridView中添加新的空行?

时间:2015-08-17 07:33:19

标签: c# asp.net gridview

当DataSource表绑定到包含数据的GridView时。可以轻松加载数据并添加新行以输入新数据。但是当DataSource表为空时,我无法向GridView输入新数据,因为AddNew命令按钮不显示。当表源为空以输入新数据时,如何在GridView中添加新的空行? 如果数据源表不为空,我的代码运行良好:

 protected void grvEmployeeOnLeave_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                DropDownList ddlEmpID = (DropDownList)e.Row.FindControl("ddlEmpID");
                if (ddlEmpID != null)
                {
                    ddlEmpID.DataSource = EmployeeService.Employee_Working();
                    ddlEmpID.DataBind();

                    ddlEmpID.SelectedValue = grvEmployeeOnLeave.DataKeys[e.Row.RowIndex].Values[1].ToString();
                }
                DropDownList ddlLeaveTypeID = (DropDownList)e.Row.FindControl("ddlLeaveTypeID");
                if (ddlLeaveTypeID != null)
                {
                    ddlLeaveTypeID.DataSource = LeaveTypeService.LeaveType_GetByAll2();

                    ddlLeaveTypeID.SelectedValue = grvEmployeeOnLeave.DataKeys[e.Row.RowIndex].Values[1].ToString();
                }
            }

            if (e.Row.RowType == DataControlRowType.Footer)
            {

                DropDownList ddlNewEmpID = (DropDownList)e.Row.FindControl("ddlNewEmpID");
                ddlNewEmpID.DataSource = EmployeeService.Employee_Working();

                ddlNewEmpID.DataBind();
                DropDownList ddlNewLeaveTypeID = (DropDownList)e.Row.FindControl("ddlNewLeaveTypeID");
                ddlNewLeaveTypeID.DataSource = LeaveTypeService.LeaveType_GetByAll2();

                ddlNewLeaveTypeID.DataBind();
                e.Row.Cells[6].Text = (grvEmployeeOnLeave.PageIndex + 1) + " of " + grvEmployeeOnLeave.PageCount;
            }
        }
        protected void grvEmployeeOnLeave_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton delButton = (ImageButton)e.Row.FindControl("lbtDelete");
                delButton.Attributes.Add("onclick", "this.originalcolor=this.style.backgroundColor;" + " this.parentNode.parentNode.style.backgroundColor='#f4a396'; if (confirm('Are you sure you want to delete this entry?')) return true; else {this.parentNode.parentNode.style.backgroundColor=this.originalcolor; return false;}");
            }
        }
        protected void grvEmployeeOnLeave_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("AddNew"))
            {
                TextBox txtNewLeaveDate = (TextBox)grvEmployeeOnLeave.FooterRow.FindControl("txtNewLeaveDate");
                TextBox txtNewNotes = (TextBox)grvEmployeeOnLeave.FooterRow.FindControl("txtNewNotes");
                DropDownList ddlNewEmpID = (DropDownList)grvEmployeeOnLeave.FooterRow.FindControl("ddlNewEmpID");
                DropDownList ddlNewLeaveTypeID = (DropDownList)grvEmployeeOnLeave.FooterRow.FindControl("ddlNewLeaveTypeID");

                Data.EmployeeOnLeave obj = new Data.EmployeeOnLeave();
                obj.LeaveDate = txtNewLeaveDate.Text;
                obj.Notes = txtNewNotes.Text;
                obj.EmpID = ddlNewEmpID.SelectedValue;
                obj.LeaveTypeID = ddlNewLeaveTypeID.SelectedValue;
                EmployeeOnLeaveService.EmployeeOnLeave_Insert(obj);
                BindGrid();
            }
        }
        protected void grvEmployeeOnLeave_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvEmployeeOnLeave.EditIndex = e.NewEditIndex;
            BindGrid();
        }
        protected void grvEmployeeOnLeave_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvEmployeeOnLeave.EditIndex = -1;
            BindGrid();
        }
        protected void grvEmployeeOnLeave_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtLeaveDate = (TextBox)grvEmployeeOnLeave.Rows[e.RowIndex].FindControl("txtLeaveDate");
            DropDownList ddlEmpID = (DropDownList)grvEmployeeOnLeave.Rows[e.RowIndex].FindControl("ddlEmpID");
            DropDownList ddlLeaveTypeID = (DropDownList)grvEmployeeOnLeave.Rows[e.RowIndex].FindControl("ddlLeaveTypeID");
            TextBox txtNotes = (TextBox)grvEmployeeOnLeave.Rows[e.RowIndex].FindControl("txtNotes");

            if (Page.IsValid)
            {
                Data.EmployeeOnLeave obj = new Data.EmployeeOnLeave();
                string Id = grvEmployeeOnLeave.DataKeys[e.RowIndex].Values[0].ToString();
                obj.Id = Id;
                obj.LeaveDate = txtLeaveDate.Text;
                obj.EmpID = ddlEmpID.SelectedValue;
                obj.LeaveTypeID = ddlLeaveTypeID.SelectedValue;
                obj.Notes = txtNotes.Text;
                EmployeeOnLeaveService.EmployeeOnLeave_Update(obj);
            }
            grvEmployeeOnLeave.EditIndex = -1;
            BindGrid();
        }
        protected void grvEmployeeOnLeave_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string Id = grvEmployeeOnLeave.DataKeys[e.RowIndex].Values[0].ToString();
            EmployeeOnLeaveService.EmployeeOnLeave_Delete(Id);
            BindGrid();
        }

有数据时: enter image description here

当datasource为空时: enter image description here

2 个答案:

答案 0 :(得分:0)

我问我回答哈哈:)。对于像我这样的问题,这是100%的工作: Firts创建EmpyDataTemplate

..................................
</Columns> 

                                            <EmptyDataTemplate>
                                              <table class="table table-striped table-bordered table-hover">
                                                <tr>
                                                  <td>Employee Name</td>
                                                  <td>Leave Date</td>
                                                  <td>Leave Type</td>
                                                  <td>Notes</td>
                                                </tr>
                                                <tr>
                                                  <td>
                                                    <asp:DropDownList ID="ddlNewEmpID" runat="server" DataTextField="EmpName" DataValueField="Id" CssClass="ddl-boxname"> </asp:DropDownList> 
                                                  </td>
                                                  <td>
                                                    <asp:TextBox ID="txtNewLeaveDate" runat="server" CssClass="textnumber"></asp:TextBox>
                                                      <asp:MaskedEditExtender ID="meetxtNewLeaveDate" runat="server" Mask="99/99/9999" MaskType="Date" OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError" TargetControlID="txtNewLeaveDate"  Century="2000"/> 
                                                     <asp:MaskedEditValidator ID="mevtxtNewLeaveDate" runat="server" ControlExtender="meetxtNewLeaveDate" ControlToValidate="txtNewLeaveDate" Display="Dynamic" EmptyValueBlurredText="Date  is required" IsValidEmpty="True" InvalidValueBlurredMessage="Date format is invalid" SetFocusOnError="True"/>
                                                  </td>
                                                  <td>
                                                    <asp:DropDownList ID="ddlNewLeaveTypeID" runat="server" DataTextField="LeaveTypeName" DataValueField="Id" CssClass="form-control"> </asp:DropDownList> 
                                                  </td>
                                                  <td>
                                                    <asp:TextBox ID="txtNewNotes" runat="server" CssClass="textnumber"></asp:TextBox>
                                                  </td>
                                                  <td>
                                                    <asp:ImageButton ID="lbtAddNew" runat="server" ToolTip="Add New" ImageUrl="/App_Themes/Admin/img/file_add.png" CausesValidation="False" OnClick="AddNew_Click"  />
                                                  </td>
                                                </tr>
                                              </table>

                                        </EmptyDataTemplate>
                                       <FooterStyle CssClass="Control" />
                                        <pagerstyle   ForeColor="black"  
                                        HorizontalAlign="Center"></pagerstyle>
                               </asp:GridView>

然后:

protected void AddNew_Click(object sender, EventArgs e)
        {
                DropDownList ddlNewEmpID = grvEmployeeOnLeave.Controls[0].Controls[0].FindControl("ddlNewEmpID") as DropDownList;
                DropDownList ddlNewLeaveTypeID = grvEmployeeOnLeave.Controls[0].Controls[0].FindControl("ddlNewLeaveTypeID") as DropDownList;
                TextBox txtNewLeaveDate = grvEmployeeOnLeave.Controls[0].Controls[0].FindControl("txtNewLeaveDate") as TextBox;
                TextBox txtNewNotes = grvEmployeeOnLeave.Controls[0].Controls[0].FindControl("txtNewNotes") as TextBox;
                Data.EmployeeOnLeave obj = new Data.EmployeeOnLeave();
                obj.LeaveDate = txtNewLeaveDate.Text;
                obj.Notes = txtNewNotes.Text;
                obj.EmpID = ddlNewEmpID.SelectedValue;
                obj.LeaveTypeID = ddlNewLeaveTypeID.SelectedValue;
                EmployeeOnLeaveService.EmployeeOnLeave_Insert(obj);
                BindGrid();

        }

然后:

protected void grvEmployeeOnLeave_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            {
                DropDownList ddlNewEmpID = (DropDownList)e.Row.FindControl("ddlNewEmpID");
                ddlNewEmpID.DataSource = EmployeeService.Employee_Working();

                ddlNewEmpID.DataBind();
                DropDownList ddlNewLeaveTypeID = (DropDownList)e.Row.FindControl("ddlNewLeaveTypeID");
                ddlNewLeaveTypeID.DataSource = LeaveTypeService.LeaveType_GetByAll2();

                ddlNewLeaveTypeID.DataBind();

            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {...................................................
}

完美无缺。

答案 1 :(得分:-1)

在绑定Gridview控件时,您可以检查数据表中是否存在任何行,否则您可以将默认行添加为&#34;无数据&#34;。

通过这个你可以得到一个defualt记录到你的Gridview,所以从你的页脚可以添加新的记录

 if (dt.Rows.Count == 0)
 {
            DataRow drow = dt.NewRow();
            drow[0] = "No data";
            dt.Rows.Add(drow);
 }

  grvEmployeeOnLeave.DataSource = dt;
  grvEmployeeOnLeave.DataBind();