views:

431

answers:

3

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.

+2  A: 

Use onrowediting and onrowupdating in gridview markup... some thing like

  <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">

EDIT: Hey I am not very sure abt 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 connection 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.

Wondering
Can you plz provide me the code-behind also?
JMSA
I also need to eliminate SqlDataSource and Use Winforms style SqlCommand, SqlCennection, etc.
JMSA
Edited the post
Wondering
A: 

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

e.Row.Cells(i).Text = value
michele
Errr, yucky ='(
rlb.usa
+1  A: 

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&amp;q=objectdatasource+asp.net

Cleiton
Coz I am from C# Winforms background.
JMSA