gridview edit the column, the lower left corner of the "auto-generated fields" check box to remove the hook
add boundfield (bound column) set its datafield productname, headertext set to "Name"
then add commadfield under edit, update, cancel
OK
. cs file written in a bind gridview by methods of data:
public void bind ()
{
SqlConnection conn = new SqlConnection ("server =.; uid = sa; pwd =; database = Northwind ");
conn.Open ();
SqlDataAdapter sda = new SqlDataAdapter ("select * from products", conn);
DataSet ds = new DataSet ();
sda.Fill (ds);
conn.Close ();
this.GridView1.DataSource = ds;
this.GridView1.DataBind ();
}
in the load event called bind () method .......
to the gridview datakeynames set productid (and datalist of datakeyfield bit like)
1. Editor:
find the gridview RowEditing Event:
protected void GridView1_RowEditing (object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; / / Edit the line to set the current line of action for the mouse
bind (); / / must be bound
;}
2. Updated: Locate the gridview RowUpdating event:
protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{
{
int i = Convert.ToInt32 (GridView1.DataKeys [e.RowIndex]. Value); / / key code: Take the currently selected row product id
string productname = ((TextBox) (GridView1.Rows [e.RowIndex]. Cells [0]. Controls [0 ])). Text; / / key code: Take the edit box Name (rehabilitated name), Rows [e.RowIndex]. Cells [0]. Controls [0] indicates the current row, first column (cell [ 0]) of a control (control [0])
SqlConnection conn = new SqlConnection ("server =.; uid = sa; pwd =; database = Northwind ");
conn.Open ();
string strSQL = "update products set productname = '" + productname + "' where productid = '" + i + "'";
SqlCommand cmd = new SqlCommand (strSQL, conn);
int h = cmd.ExecuteNonQuery ();
if (h> 0)
{
Response.Write ("");
GridView1 . EditIndex = -1; / / must be set, otherwise still in edit mode
}
else
Response.Write ("");
bind ();
}
}
3. Cancellation: Find the gridview RowCancelingEdit Event:
protected void GridView1_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; < br /> bind (); / / finally, rebind, or otherwise presented "Update" and "Cancel"
}
4. Collate:
to the gridview allowpaging set to true, pagesize own definition as 5
find the gridview PageIndexChanging Event:
protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind (); / / must be re-bound, or is no effect
}
5.gridview of rowcommand event take the primary key :/ / template column to put a button and put his commandname set to show
protected void GridView1_RowCommand (object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == " ; show ")
{
/ / GridViewRow drv = ((GridViewRow) (((Button) (e.CommandSource)). Parent.Parent));
/ / string index = drv.RowIndex . ToString (); / / line number
/ / string admin_id = GridView1.DataKeys [Convert.ToInt32 (index) ]. Value.ToString (); / / primary key
/ / Response.Write (index + ";" + admin_id);
/ / The following methods require the button in front of CommandArgument = '<% # Eval ("admin_id")%>'
int admin_id = Convert.ToInt32 (e.CommandArgument.ToString ()); / / primary key
int index = Convert.ToInt32 (((GridViewRow) (((Button) (e.CommandSource)). Parent.Parent)). RowIndex ); / / line number
Response.Write (admin_id + ";" + index);
}
}
Thank you, I'm still somewhat confused though have an outline of what you mean
回复删除