datagrid中添加一个空行,然后设置该行的状态为编辑状态
解决方案 »
- c#中如何将字符串转化成GBK编码?
- 谁有关于多线程操作数据库的例子...?求..
- winfrom的timer控件一直在电脑上运行是否会把机器的内存耗尽
- Checkboxlist 从数据库读出数据,如何判断有几条?
- C#数组问题
- 用C#生成的一个类库,可以动态加载吗?
- 大家好谁有关于Socket服务端的程序嘛?C#的。多线程。我收到客户端要根据情况返回一个字符串回去。
- c#中timer_Tick()难以触发,高手指点(新手弱弱的问)
- 关于两个datagrid的问题,大家来看看,新手问题!!
- 请问,如何得到datagrid中某一个cell的对象的句柄?
- 关于显式接口实现的问题
- 调用dll的问题。如何使用指针。
string SelectCommand = "SELECT au_id, au_lname, au_fname from Authors";bool isEditing = false;void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { // Databind the data grid on the first request only
// (on postback, bind only in editing, paging and sorting commands) BindGrid();
}
}// ---------------------------------------------------------------
//
// DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
//void DataGrid_ItemCommand(object sender, DataGridCommandEventArgs e) { // this event fires prior to all of the other commands
// use it to provide a more graceful transition out of edit mode CheckIsEditing(e.CommandName);
}void CheckIsEditing(string commandName) { if (dgUserInfo.EditItemIndex != -1) { // we are currently editing a row
if (commandName != "Cancel" && commandName != "Update") { // user's edit changes (if any) will not be committed
Message.Text = "Your changes have not been saved yet. Please press update to save your changes, or cancel to discard your changes, before selecting another item.";
isEditing = true;
}
}
}void DataGrid_Edit(object sender, DataGridCommandEventArgs e) { // turn on editing for the selected row if (!isEditing) {
dgUserInfo.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
}void DataGrid_Update(object sender, DataGridCommandEventArgs e) { // update the database with the new values // get the edit text boxes
string id = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
string lname = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
string fname = ((TextBox)e.Item.Cells[4].Controls[0]).Text; // TODO: update the Command value for your application
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand UpdateCommand = new SqlCommand();
UpdateCommand.Connection = myConnection; if (AddingNew)
UpdateCommand.CommandText = "INSERT INTO authors(au_id, au_lname, au_fname, contract) VALUES (@au_id, @au_lname, @au_fname, 0)";
else
UpdateCommand.CommandText = "UPDATE authors SET au_lname = @au_lname, au_fname = @au_fname WHERE au_id = @au_id"; UpdateCommand.Parameters.Add("@au_id", SqlDbType.VarChar, 11).Value = id;
UpdateCommand.Parameters.Add("@au_lname", SqlDbType.VarChar, 40).Value = lname;
UpdateCommand.Parameters.Add("@au_fname", SqlDbType.VarChar, 20).Value = fname; // execute the command
try {
myConnection.Open();
UpdateCommand.ExecuteNonQuery();
}
catch (Exception ex) {
Message.Text = ex.ToString();
}
finally {
myConnection.Close();
} // Resort the grid for new records
if (AddingNew) { dgUserInfo.CurrentPageIndex = 0;
AddingNew = false;
} // rebind the grid
dgUserInfo.EditItemIndex = -1;
BindGrid();
}void DataGrid_Cancel(object sender, DataGridCommandEventArgs e) { // cancel editing dgUserInfo.EditItemIndex = -1;
BindGrid(); AddingNew = false;
}void DataGrid_Delete(object sender, DataGridCommandEventArgs e) { // delete the selected row if (!isEditing) { // the key value for this row is in the DataKeys collection
string keyValue = (string)dgUserInfo.DataKeys[e.Item.ItemIndex]; // TODO: update the Command value for your application
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand DeleteCommand = new SqlCommand("DELETE from authors where au_id='" + keyValue + "'", myConnection); // execute the command
myConnection.Open();
DeleteCommand.ExecuteNonQuery();
myConnection.Close(); // rebind the grid
dgUserInfo.CurrentPageIndex = 0;
dgUserInfo.EditItemIndex = -1;
BindGrid();
}
}void DataGrid_Page(object sender, DataGridPageChangedEventArgs e) { // display a new page of data if (!isEditing) {
dgUserInfo.EditItemIndex = -1;
dgUserInfo.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
}void AddNew_Click(Object sender, EventArgs e) { // add a new row to the end of the data, and set editing mode 'on' CheckIsEditing(""); if (!isEditing) { // set the flag so we know to do an insert at Update time
AddingNew = true; // add new row to the end of the dataset after binding // first get the data
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds); // add a new blank row to the end of the data
object[] rowValues = { "", "", "" };
ds.Tables[0].Rows.Add(rowValues); // figure out the EditItemIndex, last record on last page
int recordCount = ds.Tables[0].Rows.Count;
if (recordCount > 1)
recordCount--;
dgUserInfo.CurrentPageIndex = recordCount/dgUserInfo.PageSize;
dgUserInfo.EditItemIndex = recordCount%dgUserInfo.PageSize; // databind
dgUserInfo.DataSource = ds;
dgUserInfo.DataBind();
}
}// ---------------------------------------------------------------
//
// Helpers Methods:
//// property to keep track of whether we are adding a new record,
// and save it in viewstate between postbacksprotected bool AddingNew { get {
object o = ViewState["AddingNew"];
return (o == null) ? false : (bool)o;
}
set {
ViewState["AddingNew"] = value;
}
}void BindGrid() { SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection); DataSet ds = new DataSet();
myCommand.Fill(ds); dgUserInfo.DataSource = ds;
dgUserInfo.DataBind();
}
<head>
</head>
<body style="FONT-FAMILY: arial">
<h2>Editable Data Grid
</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="dgUserInfo" runat="server" DataKeyField="au_id" OnItemCommand="DataGrid_ItemCommand" OnEditCommand="DataGrid_Edit" OnUpdateCommand="DataGrid_Update" OnCancelCommand="DataGrid_Cancel" OnDeleteCommand="DataGrid_Delete" AllowPaging="true" PageSize="6" OnPageIndexChanged="DataGrid_Page" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1" width="80%">
<FooterStyle backcolor="#C6C3C6"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="White" backcolor="#4A3C8C"></HeaderStyle>
<PagerStyle font-size="Smaller" horizontalalign="Right" backcolor="#C6C3C6" mode="NumericPages"></PagerStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit">
<ItemStyle font-size="Smaller" width="10%"></ItemStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete">
<ItemStyle font-size="Smaller" width="10%"></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid>
<br />
<asp:LinkButton id="LinkButton1" onclick="AddNew_Click" runat="server" Text="Add new item" Font-Size="smaller"></asp:LinkButton>
<br />
<br />
<asp:Label id="Message" runat="server" width="80%" forecolor="red" enableviewstate="false"></asp:Label>
</form>
</body>
</html>
jsp 中用js 能实现
可是datagird 想知道?
UP