473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Row Updation In Grid View Using C#

I want to update a row in grid view using Bound column
my code is like this :::But it does`nt work

int iRowIndex = e.RowIndex;
string sOrderNo = GridView1.Rows[iRowIndex].Cells[1].Text;

string sOrdeDate = GridView1.Rows[iRowIndex].Cells[2].Text.ToString();
it does`nt gives us value of cell???????
Mar 12 '08 #1
2 1450
saran23
28
I want to update a row in grid view using Bound column
my code is like this :::But it does`nt work

int iRowIndex = e.RowIndex;
string sOrderNo = GridView1.Rows[iRowIndex].Cells[1].Text;

string sOrdeDate = GridView1.Rows[iRowIndex].Cells[2].Text.ToString();
it does`nt gives us value of cell???????

Instead of e.RowIndex try GrdviewName.SelectedIndex,
Hope it works.
Mar 12 '08 #2
hi amit,


I think this code will be useful for u!!!!!!!!!!!!!!!

This code is used for Updating a database (MS SQL) from a grid view
This Coantains the code for editing and cancelling


Anyway try this

///////****************CODE


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridViewTemplateFieldEdit : System.Web.UI.Page
{

SqlConnection connection;
SqlCommand cmd;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ConnectAndBound();
}


}
public void ConnectAndBound()
{
// connection = new SqlConnection(" server=192.168.1.2;database=ProjectManagementSyste m;user ID=sa; password=sa");
connection = new SqlConnection(ConfigurationManager.ConnectionStrin gs["TestConnectionString"].ConnectionString);
connection.Open();
cmd = new SqlCommand("dbo.select_StudentDemo", connection);
cmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
GV_Student.DataSource = ds;
GV_Student.DataBind();
connection.Close();
}

protected void GV_Student_RowEditing(object sender, GridViewEditEventArgs e)
{
GV_Student.EditIndex = e.NewEditIndex;
ConnectAndBound();
}
protected void GV_Student_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void GV_Student_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{

GV_Student.EditIndex = -1;
ConnectAndBound();
Response.Write("Editing Cancelled");
}
protected void GV_Student_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

// connection = new SqlConnection(" server=192.168.1.2;database=ProjectManagementSyste m;user ID=sa; password=sa");
connection = new SqlConnection(ConfigurationManager.ConnectionStrin gs["TestConnectionString"].ConnectionString);
connection.Open();
cmd = new SqlCommand("dbo.Update_StudentDemo", connection);
cmd.CommandType = CommandType.StoredProcedure;


GridViewRow row = GV_Student.Rows[e.RowIndex];


Label lbl_StudentID=(Label)(row.FindControl("lbl_Student ID")as Label);
int studid = int.Parse(lbl_StudentID.Text);

TextBox txt_StudentName=(TextBox)(row.FindControl("txt_Stu dentName")as TextBox);
TextBox txt_email = (TextBox)(row.FindControl("txt_Email") as TextBox);
TextBox txt_phone = (TextBox)(row.FindControl("txt_Phone") as TextBox);
TextBox txt_AccountNo = (TextBox)(row.FindControl("txt_AccountNumber") as TextBox);


cmd.Parameters.Add(new SqlParameter("@studentid", SqlDbType.BigInt, 8));
cmd.Parameters[0].Value = studid;

cmd.Parameters.Add(new SqlParameter("@studentname", SqlDbType.VarChar, 30));
cmd.Parameters[1].Value = txt_StudentName.Text;

cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar, 30));
cmd.Parameters[2].Value = txt_email.Text;


cmd.Parameters.Add(new SqlParameter("@phone", SqlDbType.VarChar, 30));
cmd.Parameters[3].Value = txt_phone.Text;


cmd.Parameters.Add(new SqlParameter("@accountno", SqlDbType.VarChar, 20));
cmd.Parameters[4].Value = txt_AccountNo.Text;

cmd.ExecuteNonQuery();
connection.Close();
Response.Write("*********Data Updated***********");



}
protected void GV_Student_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{

}
}







/**************************************NIce C#ing
Mar 12 '08 #3

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

Similar topics

5
by: pmud | last post by:
Hi, I need to display columns in a data grid based on 7 different queries. Now I have 32 questions: 1. Is it possble to have 1 single data adapter with 7 queries & 1 data set or do I need to...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
1
by: AA | last post by:
We want to edit, update data of DataGrid in Asp.Net with VB.Net.and are writing the following code for EditCommand and UpdateCommand Event is : DataGrid1_EditCommand
2
by: probashi | last post by:
Hi, Using the SqlDataSource/SelectParameters/ControlParameter one can easily bind a Grid View with a list box (or any other controls), pretty cool, but my list box is multi select. My...
0
by: smasadhaque | last post by:
We are using a third party control C1FlexGrid to display the real time data. The data is received from a server on a different thread, processed and then stored in a dataset table. This table is...
2
by: Umeshnath | last post by:
Hi, I have placed a grid view inside Atlas panel. On click of a button event, data is populated in the grid view, I want to add scroll bar instead of increasing the size of grid view. I have...
0
by: sajithamol | last post by:
Can we update the cells of the table control(just like how we perform updation in a grid view). If so how?
1
by: vvasnm | last post by:
Hi This is surya, I am struggling with one issue regarding the updation of the value in the sub form ,when the related data has been deleted from the data sheet view of the main form. I am...
1
by: siri11 | last post by:
Hi everyone!!!!!!!!! If i update a record in a table in sqlserver from front end (c#.net),using a stored procedure for updating then after updation if i open the table then it is showing the same...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.