我有一个有问题的项目,我找不到原因。 我使用Ajaxcontrol GridView来显示来自数据库的数据。
填充后的每一行都有一个 DELETE和EDIT 按钮。 单击DELETE时,应显示确认弹出窗口。 单击EDIT时,将显示ModalPopUpExtender以编辑数据。
行中的某些按钮(如前15行)有效,但下面的行按钮不起作用。见图片
我使用onRowDataBound来总结每个员工的价值和所有记录的总和
protected void gvDetails_OnRowDatabound(object sender, GridViewRowEventArgs e)
{
for (int rowIndex = gvDetails.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow gvRow = gvDetails.Rows[rowIndex];
GridViewRow gvPreviousRow = gvDetails.Rows[rowIndex + 1];
for (int cellCount = 0; cellCount < 2; //gvRow.Cells.Count
cellCount++)
{
if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
{
if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
{
gvRow.Cells[cellCount].RowSpan = 2;
}
else
{
gvRow.Cells[cellCount].RowSpan = gvPreviousRow.Cells[cellCount].RowSpan + 1;
}
gvPreviousRow.Cells[cellCount].Visible = false;
}
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
EmployeeNo = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "EmployeeNo").ToString());
double TotalPHP = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PHP_Amount").ToString());
double TotalUSD = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "USD_Amount").ToString());
string AdjType = DataBinder.Eval(e.Row.DataItem, "AdjType").ToString();
if (AdjType == "Income")
{
TotalPrice1 += TotalPHP;
TotalPrice2 += TotalUSD;
}
else
{
TotalPrice1D += TotalPHP;
TotalPrice2D += TotalUSD;
}
string sNum = e.Row.Cells[4].Text;//just changed the index of cells based on your requirements
double sum;
if (double.TryParse(sNum, out sum))
{
if (e.Row.Cells[7].Text.ToString() == "Income")
{
totalI += sum;
}
else
{
totalD += sum;
}
}
string sNum2 = e.Row.Cells[5].Text;//just changed the index of cells based on your requirements
double sum2;
if (double.TryParse(sNum2, out sum2))
{
if (e.Row.Cells[7].Text.ToString() == "Income")
{
totalI2 += sum2;
}
else
{
totalD2 += sum2;
}
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[3].Style.Add("Padding", "0px");
e.Row.Cells[3].Text = "<table align='right' style='width:194px;'><tr><td style='text-align:left;'>" + "Total Income:" + "</td></tr><tr><td style='text-align:left;'>" + "Total Deductions:" + "</td></tr></table>";
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Left;
e.Row.Cells[4].Text = "<table style='width:100px;'><tr><td style='text-align:right; width:80px;'>" + totalI.ToString("N") + "</td></tr><tr><td style='text-align:right;'>" + totalD.ToString("N") + "</td></tr></table>";
e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[5].Text = "<table style='width:100px;'><tr><td style='text-align:right; width:80px;'>" + totalI2.ToString("N") + "</td></tr><tr><td style='text-align:right;'>" + totalD2.ToString("N") + "</td></tr></table>";
e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
}
}
并且我使用这个为每个雇员插入一个行的人员
protected void gvDetails_RowCreated(object sender, GridViewRowEventArgs e)
{
bool Row = false;
if ((EmployeeNo > 0) && (DataBinder.Eval(e.Row.DataItem, "EmployeeNo") != null))
{
if (EmployeeNo != Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "EmployeeNo").ToString()))
Row = true;
}
if ((EmployeeNo > 0) && (DataBinder.Eval(e.Row.DataItem, "EmployeeNo") == null))
{
Row = true;
Index = 0;
}
if (Row)
{
GridView GridView1 = (GridView)sender;
GridViewRow NewTotalRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
NewTotalRow.Font.Bold = true;
NewTotalRow.Style.Add("Background-Color", "#dbdbdb");
NewTotalRow.Style.Add("Color", "#444444");
NewTotalRow.ForeColor = System.Drawing.Color.White;
TableCell HeaderCell = new TableCell();
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.Text = "";
HeaderCell.ColumnSpan = 3;
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Left;
HeaderCell.Style.Add("Padding", "0px");
HeaderCell.Text = "<table align='right' cellspacing='0' style='width:193px;'><tr><td style='text-align:left; width:122px;'>" + "Total Income:" + "</td></tr><tr><td style='text-align:left; width:200px;'>" + "Total Deductions:" + "</td></tr></table>";
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Style.Add("Background-Color", "#b4e7e1");
HeaderCell.Style.Add("Color", "#1C5E55");
HeaderCell.Style.Add("Padding", "0px");
HeaderCell.Text = "<table cellspacing='0' style='width:100px;'><tr><td style='text-align:right; width:80px;'>" + TotalPrice1.ToString("N") + "</td></tr><tr><td style='text-align:right;'>" + TotalPrice1D.ToString("N") + "</td></tr></table>";
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Style.Add("Background-Color", "#b5cbe7");
HeaderCell.Style.Add("Color", "#3d566f");
HeaderCell.Style.Add("Padding", "0px");
HeaderCell.Text = "<table cellspacing='0' style='width:100px;'><tr><td style='text-align:right; width:80px;'>" + TotalPrice2.ToString("N") + "</td></tr><tr><td style='text-align:right;'>" + TotalPrice2D.ToString("N") + "</td></tr></table>";
NewTotalRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.HorizontalAlign = HorizontalAlign.Right;
HeaderCell.Text = "";
HeaderCell.ColumnSpan = 3;
NewTotalRow.Cells.Add(HeaderCell);
GridView1.Controls[0].Controls.AddAt(e.Row.RowIndex + Index, NewTotalRow);
Index++;
TotalPrice1 = 0;
TotalPrice1D = 0;
TotalPrice2 = 0;
TotalPrice2D = 0;
}
}
GRIDVIEW的设计代码
<asp:GridView ID="gvDetails" CellPadding="2" runat="server" ShowHeader="True" Width="1130px" GridLines="both" AutoGenerateColumns="false" CssClass="datagrid" EmptyDataText= "No timekeeping transaction posted for this classification." DataKeyNames="EmployeeNo"
ShowFooter="True" OnRowCreated="gvDetails_RowCreated" OnRowDataBound="gvDetails_OnRowDatabound">
<EmptyDataRowStyle BackColor="#E3EAEB" BorderColor="#1C5E55" HorizontalAlign="Center" ForeColor="#4B6C9E" />
<Columns>
<asp:BoundField DataField="EmpName" HeaderText="Employee Name" ItemStyle-ForeColor="#1C5E55" ItemStyle-Font-Bold="true" SortExpression="EmpName" ItemStyle-Width="427px" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="EmployeeNo" HeaderText="Employee No" ItemStyle-Width="341px" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="AdjustmentCode" HeaderText="Adjustment Code" ItemStyle-Width="417px" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="Adjustment" HeaderText="Adjustment Name" ItemStyle-Width="240px" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="PHP_Amount" HeaderText="PHP Amount" ItemStyle-BackColor="#d1f1ed" DataFormatString="{0:n2}" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField DataField="USD_Amount" HeaderText="USD Amount" ItemStyle-BackColor="#d1dff1" DataFormatString="{0:n2}" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center"/>
<asp:BoundField DataField="TaxableNote" HeaderText="Taxable" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="CurrencyCode" HeaderText="Currency" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
<asp:BoundField DataField="AdjType" HeaderText="Adjustment Type" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center" />
<asp:TemplateField ItemStyle-Width="16">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="server" CssClass="delete-item nooutline" Width="16" Height="16" ToolTip="Delete" CommandArgument='<%# Bind("AdjustmentNo") %>' OnClick="lnkDelete_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="16">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CssClass="edit-item nooutline" Width="16" Height="16" ToolTip="Edit" CommandArgument='<%# Bind("AdjustmentNo") %>' OnClick="lnkEdit_Click"></asp:LinkButton>
<asp:HiddenField ID="hdnNotes" runat="server" Value='<%# Bind("Notes") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是编辑按钮点击事件
protected void lnkEdit_Click(object sender, EventArgs e)
{
LinkButton lnkSender = (LinkButton)sender;
hdnAdjustmentID.Value = lnkSender.CommandArgument.ToString();
GridViewRow gvRow = (GridViewRow)lnkSender.NamingContainer;
popuAdjustment();
hdnNewEdit.Value = "Edit";
divSearchNew.Visible = false;
lblEditTitle.Text = "Edit Payroll Adjustment";
HiddenField hdnNotes = (HiddenField)gvRow.FindControl("hdnNotes");
string payPeriod = cutoffClass.getActivePayrollPeriod();
txtFrom.Text = payPeriod.Split('-')[0].ToString().Trim();
txtTo.Text = payPeriod.Split('-')[1].ToString().Trim();
lblSearchEmpName.Text = gvRow.Cells[0].Text.ToString().Trim();
lblSearchEmpNo.Text = gvRow.Cells[1].Text.ToString().Trim();
txtNotes.Text = hdnNotes.Value;
string adjCode = gvRow.Cells[2].Text.ToString().Trim();
//drpAdjCode.SelectedValue = String.Compare(drpAdjCode.SelectedValue, adkCpde, false);
foreach (System.Web.UI.WebControls.ListItem li in drpAdjCode.Items)
{
if (li.Value.Trim().ToUpper() == adjCode.Trim().ToUpper())
{
li.Selected = true;
}
}
string tax = gvRow.Cells[6].Text.ToString().Trim();
foreach (System.Web.UI.WebControls.ListItem li in drptaxable.Items)
{
if (li.Text.Trim().ToUpper() == tax.ToUpper())
{
li.Selected = true;
}
else
{
li.Selected = false;
}
}
string adjType = gvRow.Cells[8].Text.ToString().Trim();
foreach (System.Web.UI.WebControls.ListItem li in rdoAdjType.Items)
{
if (li.Text.Trim().ToUpper() == adjType.ToUpper())
{
li.Selected = true;
}
else
{
li.Selected = false;
}
}
string curr = gvRow.Cells[7].Text.ToString();
drpCurr.SelectedValue = curr;
decimal amount = 0;
if (curr == "PHP")
{
amount = Convert.ToDecimal(gvRow.Cells[4].Text.ToString());
}
else if (curr == "USD")
{
amount = Convert.ToDecimal(gvRow.Cells[5].Text.ToString());
}
txtAmount.Text = amount.ToString();
modalNew.Show();
}