我有一个动态网格视图,我可以动态添加一行....代码如下所示
.aspx页面:(没有分页)
<asp:GridView ID="Grid_AccEntry" runat="server" AutoGenerateColumns="False" GridLines="None"
OnRowDeleting="Grid_AccEntry_RowDeleting" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="amount" runat="server" Width="100" Text='<%# Bind("amount") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please Enter Amount"
ValidationGroup="Validation" ControlToValidate="amount" Display="None"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DR/CR">
<ItemTemplate>
<asp:DropDownList ID="DrCr" runat="server" AutoPostBack="true">
<asp:ListItem Value="D">Debit</asp:ListItem>
<asp:ListItem Value="C">Credit</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account">
<ItemTemplate>
<asp:DropDownList ID="account" runat="server" Width="100" OnSelectedIndexChanged="accountChanged"
DataSourceID="SqlDataSource1" DataTextField="account" AutoPostBack="true"
DataValueField="acc" >
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT [Account] as [acc],[ADesc] as [account], [OpenItemC] as [opn] FROM [Account_M]">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Opr Unit">
<ItemTemplate>
<asp:DropDownList ID="oprUnit" runat="server" Width="100"
DataSourceID="SqlDataSource2" DataTextField="OUnitName"
DataValueField="OprUnit">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT [OprUnit], SUBSTRING([OUnitName],0,50) as [OUnitName] FROM [OprUnit_M]">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dept">
<ItemTemplate>
<asp:DropDownList ID="dept" runat="server" Width="100"
DataSourceID="deptSqlDataSource" DataTextField="DeptName"
DataValueField="DeptCode">
</asp:DropDownList>
<asp:SqlDataSource ID="deptSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT [DeptCode], SUBSTRING([DeptName],0,50) as [DeptName] FROM [Department_M]">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Affiliate">
<ItemTemplate>
<asp:DropDownList ID="affiliate" runat="server" Width="100"
DataSourceID="affilSqlDataSource" DataTextField="AfBUName"
DataValueField="AffilateBU">
</asp:DropDownList>
<asp:SqlDataSource ID="affilSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT [AffilateBU], SUBSTRING([AfBUName],0,50) as [AfBUName] FROM [Affilate_M]">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Open Item">
<ItemTemplate>
<asp:DropDownList ID="openItem" runat="server" Width="100"
DataSourceID="openitmSqlDataSource" DataTextField="EmpName"
DataValueField="EmpCode">
</asp:DropDownList>
<asp:SqlDataSource ID="openitmSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConStr %>"
SelectCommand="SELECT [EmpCode], SUBSTRING([EmpName],0,50) as [EmpName] FROM [OpenItem_M]">
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Line Narration">
<ItemTemplate><asp:TextBox runat="server" ID="lineNar" Width="100"></asp:TextBox></ItemTemplate>
</asp:TemplateField>
<asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
</Columns>
<HeaderStyle BackColor="#993300" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
.cs页面
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("amount", typeof(string)));
dt.Columns.Add(new DataColumn("DrCr", typeof(string)));
dt.Columns.Add(new DataColumn("account", typeof(string)));
dt.Columns.Add(new DataColumn("oprUnit", typeof(string)));
dt.Columns.Add(new DataColumn("dept", typeof(string)));
dt.Columns.Add(new DataColumn("affiliate", typeof(string)));
dt.Columns.Add(new DataColumn("openItem", typeof(string)));
dt.Columns.Add(new DataColumn("lineNar", typeof(string)));
dr = dt.NewRow();
dr["amount"] = string.Empty;
dr["DrCr"] = string.Empty;
dr["account"] = string.Empty;
dr["oprUnit"] = string.Empty;
dr["dept"] = string.Empty;
dr["affiliate"] = string.Empty;
dr["openItem"] = string.Empty;
dr["lineNar"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
Grid_AccEntry.DataSource = dt;
Grid_AccEntry.DataBind();
}
protected void addRow(object sender, EventArgs e)
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox amount = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[0].FindControl("amount");
DropDownList DrCr = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[1].FindControl("DrCr");
DropDownList account = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[2].FindControl("account");
DropDownList oprUnit = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[3].FindControl("oprUnit");
DropDownList dept = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[4].FindControl("dept");
DropDownList affiliate = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[5].FindControl("affiliate");
DropDownList openItem = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[6].FindControl("openItem");
TextBox lineNar = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["amount"] = amount.Text.Trim() == "" ? 0 : Convert.ToDecimal(amount.Text);
dtCurrentTable.Rows[i - 1]["DrCr"] = DrCr.Text;
dtCurrentTable.Rows[i - 1]["account"] = account.Text;
dtCurrentTable.Rows[i - 1]["oprUnit"] = oprUnit.Text;
dtCurrentTable.Rows[i - 1]["dept"] = dept.Text;
dtCurrentTable.Rows[i - 1]["affiliate"] = affiliate.Text;
dtCurrentTable.Rows[i - 1]["openItem"] = openItem.Text;
dtCurrentTable.Rows[i - 1]["lineNar"] = lineNar.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Grid_AccEntry.DataSource = dtCurrentTable;
Grid_AccEntry.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox amount = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[0].FindControl("amount");
DropDownList DrCr = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[1].FindControl("DrCr");
DropDownList account = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[2].FindControl("account");
DropDownList oprUnit = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[3].FindControl("oprUnit");
DropDownList dept = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[4].FindControl("dept");
DropDownList affiliate = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[5].FindControl("affiliate");
DropDownList openItem = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[6].FindControl("openItem");
TextBox lineNar = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar");
amount.Text = dt.Rows[i]["amount"].ToString();
DrCr.Text = dt.Rows[i]["DrCr"].ToString();
account.Text = dt.Rows[i]["account"].ToString();
oprUnit.Text = dt.Rows[i]["oprUnit"].ToString();
dept.Text = dt.Rows[i]["dept"].ToString();
affiliate.Text = dt.Rows[i]["affiliate"].ToString();
openItem.Text = dt.Rows[i]["openItem"].ToString();
lineNar.Text = dt.Rows[i]["lineNar"].ToString();
//to bind long narr text to gridview line narr
TextBox lineNar1 = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar");
if (lineNar1.Text == "")
lineNar1.Text = lngNar.Text;
rowIndex++;
}
}
}
}
private void SetRowData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox amount = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[0].FindControl("amount");
DropDownList DrCr = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[1].FindControl("DrCr");
DropDownList account = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[2].FindControl("account");
DropDownList oprUnit = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[3].FindControl("oprUnit");
DropDownList dept = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[4].FindControl("dept");
DropDownList affiliate = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[5].FindControl("affiliate");
DropDownList openItem = (DropDownList)Grid_AccEntry.Rows[rowIndex].Cells[6].FindControl("openItem");
TextBox lineNar = (TextBox)Grid_AccEntry.Rows[rowIndex].Cells[7].FindControl("lineNar");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["amount"] = amount.Text.Trim() == "" ? 0 : Convert.ToDecimal(amount.Text);
dtCurrentTable.Rows[i - 1]["DrCr"] = DrCr.Text;
dtCurrentTable.Rows[i - 1]["account"] = account.Text;
dtCurrentTable.Rows[i - 1]["oprUnit"] = oprUnit.Text;
dtCurrentTable.Rows[i - 1]["dept"] = dept.Text;
dtCurrentTable.Rows[i - 1]["affiliate"] = affiliate.Text;
dtCurrentTable.Rows[i - 1]["openItem"] = openItem.Text;
dtCurrentTable.Rows[i - 1]["lineNar"] = lineNar.Text;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
}
}
else
{
Response.Write("ViewState is null");
}
//SetPreviousData();
}
protected void Grid_AccEntry_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 1)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
ViewState["CurrentTable"] = dt;
Grid_AccEntry.DataSource = dt;
Grid_AccEntry.DataBind();
SetPreviousData();
amountChanged(sender, e);
}
}
}
这里一切正常......现在对于这个gridview,我想添加分页。 即如果我点击“添加”按钮,则应在最后一页的末尾添加一个新行。 我现在必须做些什么改变?