user366312
user366312

Reputation: 16908

ASP.NET GridView Cell Editing alternative

Is it possible to edit and update a GridView cell without using

<asp:TemplateField> and/or

<%# Eval("Name") %> and/or

<%# Bind("Name") %> and/or

SqlDataSource ?

NB : I want to use only C# code in the behind and SqlConnection, SqlCommand, ExecuteXXX, etc.

NB : Plz provide me code(C# and aspx) or a web-link containing code.

Upvotes: 1

Views: 1793

Answers (3)

Wondering
Wondering

Reputation: 5076

Use onrowediting and onrowupdating in gridview markup... something like this:

  <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" AllowPaging="true" AllowSorting="true" PageSize="5" DataKeyNames="Id"
    onpageindexchanging="GridView1_PageIndexChanging" 
    AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
    onsorting="GridView1_Sorting" onrowediting="GridView1_RowEditing">

I am not very sure about winforms, but in websites try this..

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    //your code that will edit/update.

}

In general, code with Sqlcommand and Sqlconnection will be something like:

 SqlConnection con;
SqlCommand cmd;
DataSet ds;
SqlDataAdapter da;


 protected DataSet FillDataSet()
{
    string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
    con = new SqlConnection(source);
    cmd = new SqlCommand("proc_mygrid", con);
    ds = new DataSet();
    da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    return ds;


}

Hope this helps.

Upvotes: 2

Cleiton
Cleiton

Reputation: 18113

I still don't figure out why are you fighting against asp.net, but you can do what you want using ObjectDataSource. The ObjectDataSource control uses reflection to call methods of a business object to select, update, insert, and delete data. You set the ObjectDataSource control's TypeName property to specify the name of the class to use as a source object.

Working with ObjectDataSource resume to do things like:

To Declare it:

<asp:objectdatasource
  runat="server"
  id="ObjectDataSource1"
  typename="EmployeeLogic"
  selectmethod="GetAllEmployees"
  updatemethod="UpdateEmployeeInfo"
  dataobjecttypename="NorthwindEmployee" />

To Create Methods into code-behind:

public List<NorthwindEmployee>GetAllEmployees()
{
 //your code here
}
public void UpdateEmployeeInfo(NorthwindEmployee emp) {
 //your code here
}

to Configure the GridView and to be happy :)

Below I've provided some links that might help you:

http://www.manuelabadia.com/blog/PermaLink,guid,c72852ae-1fdd-4934-a715-f565ceaf21cc.aspx

http://msdn.microsoft.com/en-us/library/57hkzhy5.aspx

http://www.google.com.br/search?hl=en-us&q=objectdatasource+asp.net

Upvotes: 1

michele
michele

Reputation: 2091

A possible solution maybe is to manage the RowDataBound event of the gridview and set the values like

e.Row.Cells(i).Text = value

Upvotes: 0

Related Questions