Menu
Home
About Me
Articles
Hobbies
Code Library
Flash Library
3d stuido max

 
public void AddNewRow(object sender, System.EventArgs e)
{
  dgSearchResults.DataBind();
  DataSet ds = (DataSet) Session["dgSearchResults"];
  DataTable dt = ds.Tables[ds.Tables[0].TableName];
  DataRow dr = dt.NewRow();
  dt.Rows.Add(dr);
  dgSearchResults.EditItemIndex = dgSearchResults.Items.Count;

  // Bind the data
  dgSearchResults.DataSource = ds.Tables[0];

  // Display the data
  dgSearchResults.DataBind();
  Session["newrecord"] = true;
}

public void EditCommand(Object sender, DataGridCommandEventArgs e)
{
  dgSearchResults.EditItemIndex = e.Item.ItemIndex;
  dgSearchResults.DataBind();

}

public void CancelCommand(Object sender, DataGridCommandEventArgs e)
{
  DataSet ds = (DataSet) Session["dgSearchResults"];
  DataTable dt = ds.Tables[ds.Tables[0].TableName];
  DataRow drLast = dt.Rows[dt.Rows.Count-1];
  if (drLast.RowState == DataRowState.Added)
    drLast.RejectChanges();

  dgSearchResults.EditItemIndex = -1;
  dgSearchResults.DataBind();
}

public void UpdateCommand(Object sender, DataGridCommandEventArgs e)
{
 
  bool newrecord=false;
  int KeyID=0;
  System.Data.DataSet ds = new DataSet();
  int griditem  = dgSearchResults.EditItemIndex+2;
  string NonKeyField = "dgSearchResults:_ctl"+griditem.ToString()+":txtNonKeyField";
  NonKeyField = Request.Form.Get(NonKeyField)+"";;

  string NonKeyField2 = "dgSearchResults:_ctl"+griditem.ToString()+":txtNonKeyField2";
  NonKeyField2 = Request.Form.Get(NonKeyField2)+"";

  try
    {
    if ((bool)Session["newrecord"]) newrecord = true;
    }
  catch
    {
    newrecord = false;
    }

  if (newrecord)
    {
     if (NonKeyField=="")
     {
       ds = (DataSet) Session["dgSearchResults"];
       DataTable dt = ds.Tables[ds.Tables[0].TableName];
       DataRow drLast = dt.Rows[dt.Rows.Count-1];
       if (drLast.RowState == DataRowState.Added)
       drLast.RejectChanges();
     }
     else
    {
      CurrentConn.Open();
	  string insertSQL = 
	    "INSERT INTO TABLE (ID, Field1) _
		values('"+NonKeyField+"', '"+NonKeyField2+"')";
      OleDbDataAdapter adapter = new OleDbDataAdapter(insertSQL,CurrentConn);
      adapter.Fill(CurrentDS);
      CurrentConn.Close();
      adapter.Dispose();
    }
  }
  else
  {
    KeyID = (int)dgSearchResults.DataKeys[(int)e.Item.ItemIndex];
    CurrentConn.Open();
	string updateSQL = 
	  "UPDATE TABLE set ID  = '"+NonKeyField+"', _
	  Field1  = '"+NonKeyField2+"' _
	  where ID = "+KeyID.ToString());
    OleDbDataAdapter adapter = new OleDbDataAdapter(updateSQL,CurrentConn);
    adapter.Fill(ds);
    CurrentConn.Close();
    adapter.Dispose();
  }
  
  CurrentConn.Open();
  string insertSQL = 
    "SELECT ID as 'KeyID', Field1  as 'NonKeyField', _
	Field2 as 'NonKeyField2' FROM TABLE";
  OleDbDataAdapter adapter = new OleDbDataAdapter(insertSQL,CurrentConn);
  adapter.Fill(ds);
  CurrentConn.Close();
  adapter.Dispose()
  
  Session["dgSearchResults"] = ds;
  dgSearchResults.EditItemIndex = -1;
  dgSearchResults.DataSource = ds.Tables[0];
  dgSearchResults.DataBind();

}

private void Page_Load(object sender, System.EventArgs e)
{
  System.Data.DataSet ds = new DataSet();
  dgSearchResults.Visible = true;

  if (this.Page.IsPostBack) {
    ds = (DataSet) Session["dgSearchResults"];
  }
  else {
    ds = CreateDataSet("selectType","","","");
    Session["dgSearchResults"] = ds;
  }

  dgSearchResults.DataSource=ds.Tables[ds.Tables[0].TableName];
  dgSearchResults.DataBind();
  ds.Dispose();
}

<asp:DataGrid id="dgSearchResults" 
runat="server"
DataKeyfield="KeyID"
ShowFooter="True"
AutoGenerateColumns="False"
OnCancelCommand="CancelCommand"
OnUpdateCommand="UpdateCommand"
OnDeleteCommand="DeleteCommand"
OnEditCommand="EditCommand">
<Columns>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "NonKeyField") %>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="LinkButton1"
runat="server" text="Add New" OnClick="AddNewRow">
</asp:LinkButton>
</FooterTemplate>
<EditItemTemplate>
<asp:textbox runat="server" id="txtNonKeyField" Text='<%# DataBinder.Eval(Container.DataItem, "NonKeyField") %>'>
</asp:textbox>
</EditItemTemplate>
</asp:TemplateColumn> <asp:TemplateColumn HeaderText="">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "NonKeyField2") %>
</ItemTemplate> <EditItemTemplate>
<asp:textbox runat="server" id="txtNonKeyField2" Text='<%# DataBinder.Eval(Container.DataItem, "NonKeyField2") %>'>
</asp:textbox>
</EditItemTemplate>
</asp:TemplateColumn>

<asp:EditCommandColumn
ButtonType="LinkButton"
UpdateText="&lt;img src=images/OK_btn.gif"
CancelText="&lt;img src=images/Cancel_btn.gif"
EditText="&lt;img src=images/edit_btn.gif">
</asp:EditCommandColumn>
</Columns>
</asp:DataGrid>