AA BB CC DD ...
aa
...行aa能增加bb,cc,dd 列AA,BB,CC,DD能改列名和列的数量
然后录入数据
保存到表3
AA BB CC DD
aa 10 13 0 0
aa 10 14 20 0
aa 10 15 0 15
...
dd 10 19
用GridView怎么实现呀?请高手帮帮忙,最好能有样例发到我邮箱[email protected]谢谢!
aa
...行aa能增加bb,cc,dd 列AA,BB,CC,DD能改列名和列的数量
然后录入数据
保存到表3
AA BB CC DD
aa 10 13 0 0
aa 10 14 20 0
aa 10 15 0 15
...
dd 10 19
用GridView怎么实现呀?请高手帮帮忙,最好能有样例发到我邮箱[email protected]谢谢!
#region Directives
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
#endregionpublic partial class GridViewEditCell : System.Web.UI.Page
{
private const int _firstEditCellIndex = 2;
#region Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_sampleData = null;
this.GridView1.DataSource = _sampleData;
this.GridView1.DataBind();
} if (this.GridView1.SelectedIndex > -1)
{
this.GridView1.UpdateRow(this.GridView1.SelectedIndex, false);
}
}
#endregion
#region GridView1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
for (int columnIndex = _firstEditCellIndex; columnIndex < e.Row.Cells.Count; columnIndex++)
{
string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString());
e.Row.Cells[columnIndex].Attributes["onclick"] = js;
e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;";
}
}
} protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender;
switch (e.CommandName)
{
case ("SingleClick"):
int _rowIndex = int.Parse(e.CommandArgument.ToString());
int _columnIndex = int.Parse(Request.Form["__EVENTARGUMENT"]);
_gridView.SelectedIndex = _rowIndex;
_gridView.DataSource = _sampleData;
_gridView.DataBind(); this.Message.Text += "Single clicked GridView row at index " + _rowIndex.ToString()
+ " on column index " + _columnIndex + "<br />"; Control _displayControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[1];
_displayControl.Visible = false;
Control _editControl = _gridView.Rows[_rowIndex].Cells[_columnIndex].Controls[3];
_editControl.Visible = true;
_gridView.Rows[_rowIndex].Cells[_columnIndex].Attributes.Clear(); // Set focus on the selected edit control
ClientScript.RegisterStartupScript(GetType(), "SetFocus",
"<script>document.getElementById('" + _editControl.ClientID + "').focus();</script>"); if (_editControl is DropDownList && _displayControl is Label)
{
((DropDownList)_editControl).SelectedValue = ((Label)_displayControl).Text;
}
if (_editControl is TextBox)
{
((TextBox)_editControl).Attributes.Add("onfocus", "this.select()");
} break;
}
} protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView _gridView = (GridView)sender; if (e.RowIndex > -1)
{
for (int i = _firstEditCellIndex; i < _gridView.Columns.Count; i++)
{
Control _editControl = _gridView.Rows[e.RowIndex].Cells[i].Controls[3];
if (_editControl.Visible)
{
int _dataTableColumnIndex = i - 1; try
{
Label idLabel = (Label)_gridView.Rows[e.RowIndex].FindControl("IdLabel");
int id = int.Parse(idLabel.Text);
DataTable dt = _sampleData;
DataRow dr = dt.Rows.Find(id);
dr.BeginEdit();
if (_editControl is TextBox)
{
dr[_dataTableColumnIndex] = ((TextBox)_editControl).Text;
}
else if (_editControl is DropDownList)
{
dr[_dataTableColumnIndex] = ((DropDownList)_editControl).SelectedValue;
}
dr.EndEdit(); _sampleData = dt;
_gridView.SelectedIndex = -1;
_gridView.DataSource = dt;
_gridView.DataBind();
}
catch (ArgumentException)
{
this.Message.Text += "Error updating GridView row at index "
+ e.RowIndex + "<br />";
_gridView.DataSource = _sampleData;
_gridView.DataBind();
}
}
}
}
} protected void AddRow_Click(object sender, EventArgs e)
{
DataTable dt = _sampleData;
int newid = dt.Rows.Count + 1;
dt.Rows.Add(new object[] { newid, "", "", "" });
_sampleData = dt;
this.GridView1.DataSource = _sampleData;
this.GridView1.DataBind();
} #endregion #region Render Override
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
for (int columnIndex = _firstEditCellIndex; columnIndex < r.Cells.Count; columnIndex++)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00", columnIndex.ToString());
}
}
}
base.Render(writer);
} #endregion #region Sample Data private DataTable _sampleData
{
get
{
DataTable dt = (DataTable)Session["TestData"];
if (dt == null)
{
// Create a DataTable and save it to session
dt = new DataTable(); dt.Columns.Add(new DataColumn("Id", typeof(int)));
dt.Columns.Add(new DataColumn("Description", typeof(string)));
dt.Columns.Add(new DataColumn("AssignedTo", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string))); dt.Rows.Add(new object[] { 1, "Create a new project", "Declan", "Complete" });
dt.Rows.Add(new object[] { 2, "Build a demo applcation", "Olive", "In Progress" });
dt.Rows.Add(new object[] { 3, "Test the demo applcation", "Peter", "Pending" });
dt.Rows.Add(new object[] { 4, "Deploy the demo applcation", "Andy", "Pending" });
dt.Rows.Add(new object[] { 5, "Support the demo applcation", "", "Pending" }); // Add the id column as a primary key
DataColumn[] keys = new DataColumn[1];
keys[0] = dt.Columns["id"];
dt.PrimaryKey = keys; _sampleData = dt;
} return dt;
}
set
{
Session["TestData"] = value;
}
} #endregion
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewEditCell.aspx.cs" Inherits="GridViewEditCell" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Editing individual GridView cells</title>
<link href="css/basic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<h3>Editing individual GridView cells</h3>
<a href="Default.aspx">Back to Menu</a>
<p>
This example uses sample data which is stored in session.
<br />Try clicking and editing the individual GridView cells.
<br />New rows can also be added.
</p>
<asp:LinkButton runat="server" ID="AddRow" Text="Add Row" OnClick="AddRow_Click"></asp:LinkButton>
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE" AutoGenerateColumns="False"
BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical"
OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating" ShowFooter="True">
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False"/>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
<asp:TextBox ID="Description" runat="server" Text='<%# Eval("Description") %>' Width="175px" visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned To">
<ItemTemplate>
<asp:Label ID="AssignedToLabel" runat="server" Text='<%# Eval("AssignedTo") %>'></asp:Label>
<asp:DropDownList ID="AssignedTo" runat="server" Visible="false" AutoPostBack="true">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Andy</asp:ListItem>
<asp:ListItem>Betty</asp:ListItem>
<asp:ListItem>Conor</asp:ListItem>
<asp:ListItem>Declan</asp:ListItem>
<asp:ListItem>Eamon</asp:ListItem>
<asp:ListItem>Fergal</asp:ListItem>
<asp:ListItem>Gordon</asp:ListItem>
<asp:ListItem>Helen</asp:ListItem>
<asp:ListItem>Iris</asp:ListItem>
<asp:ListItem>John</asp:ListItem>
<asp:ListItem>Kevin</asp:ListItem>
<asp:ListItem>Lorna</asp:ListItem>
<asp:ListItem>Matt</asp:ListItem>
<asp:ListItem>Nora</asp:ListItem>
<asp:ListItem>Olive</asp:ListItem>
<asp:ListItem>Peter</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
<asp:DropDownList ID="Status" runat="server" Visible="false" AutoPostBack="true">
<asp:ListItem>Pending</asp:ListItem>
<asp:ListItem>In Progress</asp:ListItem>
<asp:ListItem>Complete</asp:ListItem>
<asp:ListItem>Cancelled</asp:ListItem>
<asp:ListItem>Suspended</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F7DE" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#6B696B" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<br /><br />
<asp:Label id="Message" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
</div>
</form>
</body>
</html>