By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

How To Update Particular Row In GridView Using C#

P: 16
Dear Sir.


Pls Give Me any One Ans how to update particular Row in GridView using C#


Regards
atul singh
Jul 14 '07 #1
Share this Question
Share on Google+
12 Replies


shweta123
Expert 100+
P: 692
Hi,

Can you please refer this

Link
Jul 14 '07 #2

P: 16
Hi,

Can you please refer this

Link

I know how to update with HTML, But I want in C# How To Update

protected void PriceEp_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}
How to Update with The Help Of Csharp.Pls Any one Give Me Proper Solutions.


Regards
atul singh
Jul 16 '07 #3

P: 3
Dear Sir.


Pls Give Me any One Ans how to update particular Row in GridView using C#


Regards
atul singh

in gridview property go to Events there doublic click of RowUpdating Event which will automaticall redirect into code behind...Inside the RowUpdating event u can code for updation

ex:
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

int i ;
i = e.RowIndex;

so i contain rowindex now u can update appropriate row according to u r Stored Procedure
Jul 16 '07 #4

P: 22
I know how to update with HTML, But I want in C# How To Update

protected void PriceEp_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}
How to Update with The Help Of Csharp.Pls Any one Give Me Proper Solutions.


Regards
atul singh
Hi
In the rowupdating event
you write like this

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");



TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");

//Your updation query pass the values like set city=' "+txtCity.Text+" '

GridView1.EditIndex = -1; //converts to normal mode from edit mode
// If having any bind method call here to see the updated rows

BindData();//(in this method write binding datasource to GridView1)


}
Thanks & Regards
Srilaskhmi.Mopidevi
Jul 16 '07 #5

P: 16
I am written This Code Pls tell Me where Is problem...
Pls Give me Full Code...

pls help me
protected void PriceEp_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

TextBox t1 = (TextBox)PriceEp.Rows[e.RowIndex].FindControl("t1");
PriceEp.EditIndex = -1;
string str = "Update EpTrans set EpCharge='"+t1.Text+"'";
EPCharge();
}

Regards
atul singh
Jul 17 '07 #6

shweta123
Expert 100+
P: 692
Hi,

Can you Please refer this Link
Jul 17 '07 #7

100+
P: 375
Hi
In the rowupdating event
you write like this

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");



TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");

//Your updation query pass the values like set city=' "+txtCity.Text+" '

GridView1.EditIndex = -1; //converts to normal mode from edit mode
// If having any bind method call here to see the updated rows

BindData();//(in this method write binding datasource to GridView1)


}
Thanks & Regards
Srilaskhmi.Mopidevi
Thank you very much SriLakshmi
Infact i had posted a query regarding the same a few backs ago.
i went through for loop and if loop and did all things which were of total failure
i came across this right now.
My problems solved.
The code which you provided roughly a fortnight has done wonders for me.
Thanks a bunch to you and the scripts.com
Regards
Hema
Aug 6 '07 #8

P: 1
Dear Sir.


Pls Give Me any One Ans how to update particular Row in GridView using C#


Regards
atul singh
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''
reply From Brianbk
Briankorobele@webmail.co.za
The code it update Datagridview
believe me was running try this one

Dim constr As String = "Data Source=localhost;Integrated Security=True"
Dim sqlstr As String = "select * from Customers"
Dim dt As New DataTable()
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
'Display the Table with data in the gridview
'clear the current content of the Table
dt.Clear()
'fill the datatable with data in the Database
Dim dpt As New SqlDataAdapter(sqlstr, constr)
dpt.Fill(dt)
dpt.Dispose()
'display the Table in the datagrid
datagridview1.DataSource =dt
End Sub

End Class
Aug 18 '07 #9

P: 4
Hi
In the rowupdating event
you write like this

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");



TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");

//Your updation query pass the values like set city=' "+txtCity.Text+" '

GridView1.EditIndex = -1; //converts to normal mode from edit mode
// If having any bind method call here to see the updated rows

BindData();//(in this method write binding datasource to GridView1)


}
Thanks & Regards
Srilaskhmi.Mopidevi

Good day... i have a problem with this code which i used it before and it really works. But when i used it again this code it returns the old value... but i dont know why?

In the gridview i set the column for example City column into a template and set the name to txtCity. Then i add the in edit column GridView1.EditIndex = -1; and bind it again.

Now the code of GridView rowupdating events is:

TextBox txtCity = (TextBox)GridView1.(TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");


and i assume that the txtCity.Text was the updated value but the old value returns. i dont know why it happen. i compared also my source code into the other which is working but there is no difference from it. Can you help me from this?
Aug 22 '07 #10

P: 19
Related to this problem, but much worse:

I have a GridView, which isn't bound in the ways mentioned above; I need to use a custom data source, and create the data grid cells programmatically from it. So far so good; output works without a glitch.

But within the RowUpdate event handler, everything goes null. A Row[ i].Cells[ j] prints nothing, event.OldValues and NewValues both have length 0, and the TextBox approach described above gives a null reference after conversion.

Long story short, I'm out of ideas.

I've posted the ASP Code, and decided to skip the actual binding code, since it works great; checked it with the debugger.
Expand|Select|Wrap|Line Numbers
  1.       <asp:GridView
  2.          ID="grdContacts" runat="server" ForeColor="#111111" BackColor="#eeeeee"
  3.          AllowPaging="True" AllowSorting="True" PageSize="3" PageIndex="0"
  4.          OnPageIndexChanging="GridView_PageIndexChanging" 
  5.          OnPageIndexChanged ="GridView_PageIndexChanged"
  6.          OnRowEditing       ="GridView_RowEditing"
  7.          OnRowUpdated       ="GridView_RowUpdated"
  8.          OnRowUpdating      ="GridView_RowUpdating"
  9.          OnRowDeleted       ="GridView_RowDeleted"
  10.          OnRowDeleting      ="GridView_RowDeleting"
  11.          OnSorting          ="GridView_Sorting"
  12.          OnRowCancelingEdit ="GridView_RowCancelingEdit"
  13.          OnRowCommand       ="GridView_RowCommand">
  14.          <emptydatarowstyle backcolor="Silver" forecolor="Red"/>                    
  15.             <emptydatatemplate>
  16.             No Data Found.
  17.             </emptydatatemplate>
  18.          <Columns>
  19.              <asp:CommandField ShowInsertButton="true" ShowDeleteButton="True" ShowSelectButton="True" ShowEditButton="True" />
  20.          </Columns>
  21.       </asp:GridView>
Aug 22 '07 #11

P: 19
It won't let me edit, so I made a new post.

I bugged some acquaintances and I managed to nail it. The code below works for my grid, and the commented code doesn't (prints nothing)

Expand|Select|Wrap|Line Numbers
  1. // all these 3 values are 0; despite what MSDN says, OldValues and NewValues
  2. // hold only air when the grid isn't bound to a standard (for them!) data
  3. // source, in a standard way.
  4.          //Response.Write(String.Format(
  5.          //   @"Updating: <br>
  6.          //   OldValues.Count = {0}<br>
  7.          //   NewValues.Count = {1}<br>
  8.          //   Keys.Count = {2} <br>"
  9.          //   , e.OldValues.Count, e.NewValues.Count, e.Keys.Count));
  10.  
Expand|Select|Wrap|Line Numbers
  1. // in my code, j=0 would try to read the CommandField; instant death.
  2.          for (int j = 1; j < grdContacts.Rows[e.RowIndex].Cells.Count; j++)
  3.          {
  4.             //Response.Write(String.Format(" From cells:: {0} /// ", grdContacts.Rows[ e.RowIndex].Cells[j].Text)); // <- shows ""
  5.             TextBox textB = (TextBox)(grdContacts.Rows[e.RowIndex].Cells[j].Controls[0]);
  6.             Response.Write(String.Format(" {0}, {1}:: {2} <br>", e.RowIndex, j, textB.Text)); // <- shows modified values.
  7.          }
  8.  
Sorry for the wrapping in the second snippet, I just don't think there's a way to make it more eye-friendly, even if I'd raze the leading spaces.

EDIT: I made a little mistake in the previous post: the event handler is RowUpdating; RowUpdated won't trigger for the life of me.
Aug 22 '07 #12

P: 1
@srilakshmim

<from singamalai>
Hello madam, my name is singamalai.
in the above code in "TextBox txtcity = (textbox)row.findcontrol("txtcity");"

i'm getting the default value which is not updated, but not updated value. please help me to update a record.

this is my code.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[index];
string nametxt = GridView1.DataKeys[e.RowIndex].Value.ToString(); //row.Cells[2].Text; //GridView1.Rows[e.RowIndex].Cells[2].Text;
TextBox txtmyname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmyname"); //((TextBox)row.FindControl("txtmyname")).Text;
string txtbname = ((TextBox)row.FindControl("txtbname")).Text;
string txtmobile = ((TextBox)row.FindControl("txtmobile")).Text;
string txtemail = ((TextBox)row.FindControl("txtemail")).Text;
con.Open();
cmd = new OleDbCommand("update reg_table set myname='" + txtmyname.Text + "',bname = '" + txtbname + "',mobile = '" + txtmobile + "', email = '" + txtemail + "' where myname='" + nametxt + "' ", con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
Response.Write("<h3> updated successfully");
else
Response.Write("<h3> Not updated");
con.Close();
//ds = (DataSet)Session["ds1"];
//ds.Tables["reg_table"].Rows[e.RowIndex]["myname"] = txtmyname;
}

please help me.

Thanks and regards,
singamalai.
simhamalai@india.com
Apr 12 '12 #13

Post your reply

Sign in to post your reply or Sign up for a free account.