Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Gridview C3
How can I add
 
DataTable dt = new DataTable();
            Dal.Fill_Dt("SELECT Tbl_NwsLtr.ID_NwsLtr , Tbl_NwsLtr.FlNm , Tbl_NwsLtr._ml,Tbl_NwsLtr._MbN, Tbl_Job.JbName, Tbl_NwsLtr._dt FROM Tbl_NwsLtr INNER JOIN Tbl_Job ON Tbl_NwsLtr._Jb_ID = Tbl_Job._Jb_ID", dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
this is relation between tables:
<img src='http://images.uvl.ir/img/e5eeca522e12.png' alt='Image Hosted by Free Photo Hosting at http://www.iranxm.com/' />
 

How can I edit,update,and add data in code behinde
when the user click on the "Edit" button? I want to show field JbName into dropdownlist.
how can I do this?
 
Thanks
please help!
Posted 11-Feb-13 21:02pm
jiji26631.7K
Edited 14-Feb-13 8:16am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

AutoGenerateColumns="false"
 
like
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        </asp:GridView>
 
[Moved from 2nd solution]
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]
 
watch this sample code..
  Permalink  
v3
Comments
jiji2663 at 14-Feb-13 8:40am
   
thanks
it's work
but i get another error when i click on button "Update"
error:
nvalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
jiji2663 at 14-Feb-13 9:12am
   
solver
i use
IsPostBack in page load
jiji2663 at 14-Feb-13 9:14am
   
i have new problem's
when i edit field and ckick on button "Update"
get this error:
Object reference not set to an instance of an object
a2ulthakur at 14-Feb-13 10:06am
   
Make sure you are using correct datatypes for all the fields you are trying to update in RowUpdating event in gridview and use breakpoints
Avik Ghosh22 at 14-Feb-13 10:49am
   
<pages enableEventValidation="true"/> error come when u don't use ispostback false in your page load part.... Object reference not set to an instance of an object means there are some error in your code ..... check your code with break point...
jiji2663 at 14-Feb-13 11:08am
   
i try this
but don't work
jiji2663 at 14-Feb-13 11:09am
   
this error just for:
string id = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("ID_Order")).Text;
i change it to:
string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("ID_Order")).Text;
and
string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("ID_Order")).Text;
but get same error
Avik Ghosh22 at 14-Feb-13 11:13am
   
bro check http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html this .... main problem in your gridview...it can not recognize the textbox ... ID_Order is this textbox id???? i am sorry for my poor English...
Avik Ghosh22 at 14-Feb-13 11:15am
   
TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtboxid");
Avik Ghosh22 at 14-Feb-13 13:19pm
   
Marcus Kramer thank u sir....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

ok first of all make a function from which u will populate your gridview like this :
 public void show()
    {
        string str = "select * from table2";
        SqlDataAdapter adp = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
 
then go to your gridview and click on the smart tag from there select uption edit columns and add a new command feild "edit, update and cancel"
now go to gridview events
 
double click the RowEditing event:
 
gridShow.EditIndex = e.NewEditIndex;
      show();

 
similarly,
under RowCancelling event:
 
GridView1.EditIndex = -1;
        show();
 
under RowUpdating event:
before doing that you should know that you should convert your gridview columns into only template fields go to smart tag of gridview and click edit template and then for the columns you dont want to edit insert a label in their edititemtemplate and the fields you want to change add a textbox

 
int id =Convert.ToInt32( ((Label)GridView1.Rows[e.RowIndex].FindControl("Label4")).Text);
        string userid = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
        string pwd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
        string str = "update table2 set userid='" + userid + "',pwd='" + pwd + "' where id=" + id;
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.Connection = con;
       // con.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
       
        GridView1.EditIndex = -1;
        
        show();
 

delete is done like this:
int id = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text);
        string del = "Delete from monthly where id="+id;
        SqlCommand cmd = new SqlCommand(del, con);
        cmd.Connection = con;
 
        con.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        show();
 
just tweak the code and it will work for you.
  Permalink  
Comments
jiji2663 at 14-Feb-13 6:45am
   
thanks for your help
but i have a new problem when i run the project,se this picture:
http://www.uploadimage.in/di/39FN/2-14-2013 3-08-34 PM.png

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid