Hi,
In this article i populate the gridview with the students marks. While rendering the gridview, i get the mark1 and mark2 of the student, then i calculate total marks of the student. I am adding the student total marks in the student row as a new column with name "Total Marks" using jquery.
.aspx page
<head runat="server">
<title></title>
<script type="text/javascript" src="../Scripts/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function () {
$('table#grdvw_students tr').each(function () {
var txt1 = $(this).find('td:nth-child(3)').find('span').text(); //get the mark1
var txt2 = $(this).find('td:nth-child(4)').find('span').text(); //get the mark2
if (txt1 != "" & txt2 != "") {
var mark1 = parseInt(txt1);
var mark2 = parseInt(txt2);
var sum = mark1 + mark2; //sum of mark1 and mark2
//creating new column for total marks in each row
$(this).append("<td><span style='color:blue;'>" + sum + "</span></td>");
}
else { //for header text
$(this).append("<td><span style='color:blue;'>Total Marks</span></td>");
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdvw_students" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="Student Id">
<ItemTemplate>
<asp:Label ID="lbl_studid" runat="server" Text='<%# Eval("StudentId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<ItemTemplate>
<asp:Label ID="lbl_name" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mark1">
<ItemTemplate>
<asp:Label ID="lbl_mark1" runat="server" Text='<%# Eval("Mark1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mark1">
<ItemTemplate>
<asp:Label ID="lbl_mark2" runat="server" Text='<%# Eval("Mark2") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
Code behind:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgridView();
}
}
private void bindgridView()
{
try
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());
List<Students> lst = new List<Students>();
lst.Add(new Students { studId = 1, Name = "Giri", Mark1 = 90,Mark2=90 });
lst.Add(new Students { studId = 2, Name = "Watson", Mark1 = 40,Mark2=50 });
lst.Add(new Students { studId = 3, Name = "Bala", Mark1 = 80,Mark2=60 });
lst.Add(new Students { studId = 4, Name = "Jim", Mark1 = 45,Mark2=45 });
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("StudentId", typeof(string));
dt.Columns.Add("Mark1", typeof(string));
dt.Columns.Add("Mark2", typeof(string));
foreach (var item in lst)
{
dr = dt.NewRow();
dr["StudentId"] = item.studId;
dr["Mark1"] = item.Mark1;
dr["Mark2"] = item.Mark2;
dt.Rows.Add(dr);
}
//add the new column in to the existing datatable
DataColumn newColumn = new DataColumn("Name",typeof(string));
newColumn.DefaultValue = "Your Project Name";
dt.Columns.Add(newColumn);
grdvw_students.DataSource = dt;
grdvw_students.DataBind();
}
catch (Exception)
{ }
}
}
public class Students
{
public int studId { get; set; }
public string Name { get; set; }
public int Mark1 { get; set; }
public int Mark2 { get; set; }
}
No comments:
Post a Comment