Add
Hi,
I have designed an aspx page in which i am having a gridview.
I used SQL server as backend and my database database name is "sample"
I created a stored procedure to select employee name from "emp" table from "sample" database and filled it to a dataset.
I had filled that gridview with that dataset and loaded employee names and Id.
I want to append a Datatable that contains only one static row into the same dataset.
If data inside "emp" table is
empId empName
1 Subha
2 Harshini
Then, during page load, along with these 2 rows, a third row as,
3 New
should be added to dataset.The output should be as below.
empId empName
1 Subha
2 Harshini
3 New
The source code I used is shown below.
In Grid.aspx.cs, the following code is used.
protected void GridView1_PreRender(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = obj.select();
GridView1.DataSource = ds;
GridView1.DataBind();
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("id", System.Type.GetType("System.Int32"));
dt.Columns.Add("name", System.Type.GetType("System.String"));
dr = dt.NewRow();
if (GridView1.Rows.Count == 0)
{
dr["id"] = Convert.ToInt32("1");
dr["name"] = "New";
}
else
{
int ct = GridView1.Rows.Count;
int hid = Convert.ToInt32(GridView1.Rows[ct].Cells[0]);
dr["id"] = hid + 1;
dr["name"] = "New";
}
dt.Rows.Add(dr);
dt = ds.Tables[1];
GridView1.DataSource = ds;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
LinkButton ln = new LinkButton();
ln = (LinkButton)GridView1.Rows[i].Cells[2].FindControl("l1");
ln.Visible = false;
}
}
In hol.cs,select method is used.
using System.Data.SqlClient;
using Majestic.Common;
public class hol:Common
{
public DataSet select()
{
DataSet ds = SqlHelper.ExecuteDataset(con, CommandType.StoredProcedure, "uspselect");
return ds;
}
}
Note: Majestic.Common dll is used for database connectivity and manipulations.
In Grid.aspx page, I used the following code inside <body> tag.
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" OnPreRender="GridView1_PreRender" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%#Eval("holid")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<%#Eval("holname")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="l1" runat="server">New</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
Can you tell if it is possible?If so, kindly give me the source code and also the guideline to solve this problem.
Thank You,
Subhashini Palanivel.
|