// isbn column (display text)
// Module Columns < / Itemtemplate> // edititeMindex pointing to the bar of the row will display this template edititemtemplate> asp: templateColumn>
// public text column
// Editing column (display as a link form)
Columns>
Ask: DataGrid>
form>
Script:
void Page_Load (Object sender, EventArgs e) {if BindDataGrid () (Page.IsPostBack!); // create data set and bind to grid control} void DoItemEdit (Object objSource, DataGridCommandEventArgs objArgs) {lblSQL.Text = ""; / / CLEAR TEXT from Label That Shows Sql Statement
// set the EditItemIndex property of the grid to this item's index // EditItemIndex attribute set to the user's line marked shock MyDataGrid.EditItemIndex = objArgs.Item.ItemIndex; BindDataGrid (); // bind the data and display it}
void DoItemUpdate (Object objSource, DataGridCommandEventArgs objArgs) {// get a reference to the title and publication date text excitation control events reference boxes //objArgs.Item the row, FindControl to find the id of txtTitle the textbox TextBox objTitleCtrl = ( TextBox) Objargs.Item.FindControl ("txttitle"); // Remove the first control in the third unit in the control of the inspiration event (because the PublisHed column is not id //, you can't use the FindControl method to find) TextBox ObjpubdateCtrl = (TextBox) objargs.Item.cells [2] .controls [0];
// create a suitable SQL statement and execute it string strSQL = "UPDATE Booklist SET Title = '" objTitleCtrl.Text "'," "PublicationDate = '" objPubDateCtrl.Text "'" "WHERE ISBN = ' " MyDataGrid.datakeys [objargs.Item.itemindex] " '"; executesqlstatement (strsql); // Database update operation
// set edititemindex property of grid to -1 to switch out of edit mode mydatagrid.edititeMindex = -1; // set to initial value binddataGrid (); // bind the data and display it}
void DoItemCancel (Object objSource, DataGridCommandEventArgs objArgs) {// set EditItemIndex property of grid to -1 to switch out of Edit mode MyDataGrid.EditItemIndex = -1; BindDataGrid (); // bind the data and display it} void ExecuteSQLStatement (string strSQL) {// this is where the SQL statement would be executed against the // original data source. in this example, we're simply displaying // the statement in a Label on the page lblSQL.Text = " The SQL Statement That Would Be EXECUTED IS: B> " strsql;}
Void BindDataGrid () {// Get Connection String from ../global/connect-strings.ascx user control string strconnect = ctlconnectstrings.oledbconnectionsTRING
// Create a SQL Statement TO SELECT SOME ROWS from The Database String Strselect = "Select * from booklist where isbn like '% 18610025%'"
// Create a variable to hold an instance of a datareader object OLEDBDATAREADER OBJDATAREADER;
Try {// create a new connection Object Using the connection = new OLEDBCONNECTION (STRCONNECT);
// Open the connection to the database objconnect.open ();
// Create A New Command Using The Connection Object and SELECT Statement OLEDBCommand Objcommand = New OLEDBCOMMAND (STRSELECT, OBJCONNECT);
// execute the SQL statement against the command to get the DataReader objDataReader = objCommand.ExecuteReader ();} catch (Exception objError) {// display error details outError.InnerHtml = " * Error while accessing data b> " " " Objerror.Source "
script> Note: TextBox Objtitlectrl = (TextBox) Objargs.Item.FindControl ("txttitle"); TextBox ObjpubdateCtrl = (TextBox) Objargs.Item.cells [2] .controls [0]; these two statements, The latter is most suitable for the column is a normal BoundColumn or an automatically generated condition (no ID) - not applicable to custom columns created with the template (this is why the Title Bar is used to use FindControl reasons (defined ID) ))