You must Sign In to post a response.

How to change value in gridview cell

I want change the value in the datagridview cell not complete the column of gridview using javascript


Comments

#705251    Author: Mahe    19 Feb 2013 Member Level: Gold   Points : 4

Hi,

Please find the code sample for changing the Column value of gridview in javascript

ASPX page


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Changing the Grid Value using the Javascript</title>
<script type="text/javascript">
function ChangeSalary() {
debugger;
var grid = document.getElementById('GridView1');

var rowCount = grid.rows.length;
var msg = "";
for (var i = 1; i < rowCount; i++) {
var empName = grid.rows[i].cells[1].innerText; //2 column corresponds to EmpName
var oldsalary = grid.rows[i].cells[3].innerText; //4 column corresponds to Salary
var newsalarycell = parseFloat(oldsalary) + 500;
grid.rows[i].cells[3].innerText = newsalarycell;

msg = msg + empName + " Salary changed from [" + oldsalary + "] to [" + newsalarycell + "]" + "\n";


}
alert(msg);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table border="1">
<tr>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Change Column Value" OnClientClick="ChangeSalary();" />
</td>
</tr>
</table>
</form>
</body>
</html>


Code behind:

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

private void LoadGrid()
{
DataTable dtEmployees = new DataTable();
dtEmployees.Columns.Add("Sno",typeof(int));
dtEmployees.Columns.Add("Name");
dtEmployees.Columns.Add("Age", typeof(int));
dtEmployees.Columns.Add("Salary", typeof(double));

DataRow dr;
dr = dtEmployees.NewRow();
dr["Sno"] = 1;
dr["Name"] = "Vijay";
dr["Age"] = 28;
dr["Salary"] = 3000.00;
dtEmployees.Rows.Add(dr);
dr = dtEmployees.NewRow();
dr["Sno"] = 2;
dr["Name"] = "Mahe";
dr["Age"] = 28;
dr["Salary"] = 3200.00;

dtEmployees.Rows.Add(dr);
dr = dtEmployees.NewRow();
dr["Sno"] = 3;
dr["Name"] = "Prasanna";
dr["Age"] = 29;
dr["Salary"] = 8200.00;

dtEmployees.Rows.Add(dr);

GridView1.DataSource = dtEmployees;
GridView1.DataBind();
}



Note: on Button click am changing the salary value.
hope this will helps

Regards,
Mahe...

Regards,Mahe
Happy Coding


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.