Step 1: ASPX Page
<asp:GridView ID="dgvVertical" AllowSorting="true" AllowPaging="true" PageSize="10"
AutoGenerateColumns="false" class="table table-bordered coursesTable" Style="width: 785px;
border-collapse: initial;" runat="server" OnRowCreated="dgvVertical_RowCreated"
OnSorting="dgvVertical_Sorting" OnPageIndexChanging="dgvVertical_PageIndexChanging"
PagerSettings-Mode="Numeric" PagerSettings-PageButtonCount="4">
<PagerStyle CssClass="gridpager" HorizontalAlign="Left" />
<RowStyle />
<AlternatingRowStyle BackColor="#f9f9f9" />
<PagerStyle CssClass="pager" />
<HeaderStyle BackColor="#D6D5D6" />
<EmptyDataTemplate>
No Record Found
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="CourseId" HeaderText="Course Id" SortExpression="CourseId"
HeaderStyle-CssClass="hideTemplateField" ItemStyle-CssClass="hideTemplateField"
ControlStyle-CssClass="hideTemplateField" />
<asp:BoundField DataField="RowNumber" HeaderText="Sr.No" SortExpression="CourseId"
HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="CourseName" HeaderText="Course Name" SortExpression="CourseName"
HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="Yearofcourse" HeaderText="Year Of The Course" SortExpression="Yearofcourse"
HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="SpecializationSubid" HeaderText="SpecializationSubid"
SortExpression="SpecializationSubid" HeaderStyle-CssClass="hideTemplateField"
ItemStyle-CssClass="hideTemplateField" ControlStyle-CssClass="hideTemplateField" />
<%-- <asp:BoundField DataField="SpecializationSubid" HeaderText="Specialization Subject ID" SortExpression="SpecializationSubid" HeaderStyle-ForeColor="Black" />--%>
<%--Commented by Shekhar 6/11/2013 to hide id on gridview page --%>
<asp:BoundField DataField="specialSubject" HeaderText="Specialization Subject Name"
SortExpression="specialSubject" HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="courseCode" HeaderText="Course Code" SortExpression="courseCode"
HeaderStyle-ForeColor="Black" Visible="false" />
<asp:TemplateField HeaderText="Edit" Visible="false">
<ItemTemplate>
<input id='btnEdit<%#Eval("CourseId") %>' type="image" title="Edit Course Details"
value="" class="icon-edit" onclick="DisplayVertical(this,'U','<%#Eval("CourseId") %>');"
style="width: 14px;" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<input id='btnDelete<%#Eval("CourseId") %>' type="image" onclick="DeleteCourse(this,'<%#Eval("CourseId") %>','D');"
title="Delete Course Details" value="" class="icon-remove" style="width: 14px;" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<table style="height: 15px; text-align: left; vertical-align: top; width: 200px;">
<tr>
<td style="width: 200px; height: 20px; text-align: center; vertical-align: middle;">
<b><i>
<%=dgvVertical.PageIndex + 1%>
of
<%=dgvVertical.PageCount%></i></b>
</td>
<td style="width: 150px; height: 20px; text-align: center; vertical-align: middle;">
<%--<input class="icon-pencil" id="btnAddNew" onmousemove="HighLight(this);" onmouseover="HighLight(this);"
type="image" value="Add New" onclick="DisplayVertical(this,'I')"style="width: 14px;" />
--%>
<input type="image" class="icon-pencil" id="Image2" value="" onclick="DisplayVertical(this,'I')"
style="width: 15px;" />
<asp:LinkButton ID="btnAddNew" Style="width: 20px;" Text="Add New" OnClientClick="DisplayVertical(this,'I')"
runat="server" />
</td>
</tr>
</table>
Step 2: Bind data to grid Code on .cs page
private void BindGrid()
{
DataSet ds = GetCourseMaster();
try
{
if (ds.Tables[0].Rows.Count > 0)
{
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortExp"] != null)
{
dv.Sort = this.ViewState["SortExp"].ToString() + " " + this.ViewState["SortOrder"].ToString();
dgvVertical.DataSource = dv.ToTable();
dgvVertical.DataBind();
}
else
{
dgvVertical.DataSource = ds;
dgvVertical.DataBind();
}
}
else
{
dgvVertical.DataSource = null;
dgvVertical.DataBind();
}
}
catch (Exception ex)
{
ErrorMessage(ex);
}
}
public DataSet GetCourseMaster()
{
string strQuery = @"SELECT row_number() OVER (ORDER BY CourseMaster.CourseName) AS RowNumber, CourseYear.Id AS CourseId, CourseMaster.CourseName, CourseYear.YearOfCourse,SubjectsMaster.SubjectName as specialSubject,
CourseYear.SpecializationSubId
FROM CourseMaster INNER JOIN
CourseYear ON CourseMaster.CourseId = CourseYear.CourseId LEFT OUTER JOIN
SubjectsMaster ON CourseYear.SpecializationSubId = SubjectsMaster.Subjectid";
using (SqlCommand cmd = GetCommand(strQuery, CommandType.Text))
{
ds = new DataSet();
SqlDataAdapter SqlAdp = new SqlDataAdapter(cmd);
SqlAdp.Fill(ds);
DBhelper.Instance.CloseConnection(cmd);
}
return ds;
}
public SqlCommand GetCommand(string spName, CommandType CommandType)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = spName;
cmd.CommandType = CommandType;
cmd.Connection = ConnectionString;
return cmd;
}
public SqlConnection ConnectionString
{
get
{
_connectionString = new SqlConnection(Getconnect());
_connectionString.Open();
return _connectionString;
}
}
public string Getconnect()
{
string connectionsring = ConfigurationManager.ConnectionStrings["Elearning"].ConnectionString;
return connectionsring;
}
<asp:GridView ID="dgvVertical" AllowSorting="true" AllowPaging="true" PageSize="10"
AutoGenerateColumns="false" class="table table-bordered coursesTable" Style="width: 785px;
border-collapse: initial;" runat="server" OnRowCreated="dgvVertical_RowCreated"
OnSorting="dgvVertical_Sorting" OnPageIndexChanging="dgvVertical_PageIndexChanging"
PagerSettings-Mode="Numeric" PagerSettings-PageButtonCount="4">
<PagerStyle CssClass="gridpager" HorizontalAlign="Left" />
<RowStyle />
<AlternatingRowStyle BackColor="#f9f9f9" />
<PagerStyle CssClass="pager" />
<HeaderStyle BackColor="#D6D5D6" />
<EmptyDataTemplate>
No Record Found
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="CourseId" HeaderText="Course Id" SortExpression="CourseId"
HeaderStyle-CssClass="hideTemplateField" ItemStyle-CssClass="hideTemplateField"
ControlStyle-CssClass="hideTemplateField" />
<asp:BoundField DataField="RowNumber" HeaderText="Sr.No" SortExpression="CourseId"
HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="CourseName" HeaderText="Course Name" SortExpression="CourseName"
HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="Yearofcourse" HeaderText="Year Of The Course" SortExpression="Yearofcourse"
HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="SpecializationSubid" HeaderText="SpecializationSubid"
SortExpression="SpecializationSubid" HeaderStyle-CssClass="hideTemplateField"
ItemStyle-CssClass="hideTemplateField" ControlStyle-CssClass="hideTemplateField" />
<%-- <asp:BoundField DataField="SpecializationSubid" HeaderText="Specialization Subject ID" SortExpression="SpecializationSubid" HeaderStyle-ForeColor="Black" />--%>
<%--Commented by Shekhar 6/11/2013 to hide id on gridview page --%>
<asp:BoundField DataField="specialSubject" HeaderText="Specialization Subject Name"
SortExpression="specialSubject" HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="courseCode" HeaderText="Course Code" SortExpression="courseCode"
HeaderStyle-ForeColor="Black" Visible="false" />
<asp:TemplateField HeaderText="Edit" Visible="false">
<ItemTemplate>
<input id='btnEdit<%#Eval("CourseId") %>' type="image" title="Edit Course Details"
value="" class="icon-edit" onclick="DisplayVertical(this,'U','<%#Eval("CourseId") %>');"
style="width: 14px;" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<input id='btnDelete<%#Eval("CourseId") %>' type="image" onclick="DeleteCourse(this,'<%#Eval("CourseId") %>','D');"
title="Delete Course Details" value="" class="icon-remove" style="width: 14px;" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<table style="height: 15px; text-align: left; vertical-align: top; width: 200px;">
<tr>
<td style="width: 200px; height: 20px; text-align: center; vertical-align: middle;">
<b><i>
<%=dgvVertical.PageIndex + 1%>
of
<%=dgvVertical.PageCount%></i></b>
</td>
<td style="width: 150px; height: 20px; text-align: center; vertical-align: middle;">
<%--<input class="icon-pencil" id="btnAddNew" onmousemove="HighLight(this);" onmouseover="HighLight(this);"
type="image" value="Add New" onclick="DisplayVertical(this,'I')"style="width: 14px;" />
--%>
<input type="image" class="icon-pencil" id="Image2" value="" onclick="DisplayVertical(this,'I')"
style="width: 15px;" />
<asp:LinkButton ID="btnAddNew" Style="width: 20px;" Text="Add New" OnClientClick="DisplayVertical(this,'I')"
runat="server" />
</td>
</tr>
</table>
Step 2: Bind data to grid Code on .cs page
private void BindGrid()
{
DataSet ds = GetCourseMaster();
try
{
if (ds.Tables[0].Rows.Count > 0)
{
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortExp"] != null)
{
dv.Sort = this.ViewState["SortExp"].ToString() + " " + this.ViewState["SortOrder"].ToString();
dgvVertical.DataSource = dv.ToTable();
dgvVertical.DataBind();
}
else
{
dgvVertical.DataSource = ds;
dgvVertical.DataBind();
}
}
else
{
dgvVertical.DataSource = null;
dgvVertical.DataBind();
}
}
catch (Exception ex)
{
ErrorMessage(ex);
}
}
public DataSet GetCourseMaster()
{
string strQuery = @"SELECT row_number() OVER (ORDER BY CourseMaster.CourseName) AS RowNumber, CourseYear.Id AS CourseId, CourseMaster.CourseName, CourseYear.YearOfCourse,SubjectsMaster.SubjectName as specialSubject,
CourseYear.SpecializationSubId
FROM CourseMaster INNER JOIN
CourseYear ON CourseMaster.CourseId = CourseYear.CourseId LEFT OUTER JOIN
SubjectsMaster ON CourseYear.SpecializationSubId = SubjectsMaster.Subjectid";
using (SqlCommand cmd = GetCommand(strQuery, CommandType.Text))
{
ds = new DataSet();
SqlDataAdapter SqlAdp = new SqlDataAdapter(cmd);
SqlAdp.Fill(ds);
DBhelper.Instance.CloseConnection(cmd);
}
return ds;
}
public SqlCommand GetCommand(string spName, CommandType CommandType)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = spName;
cmd.CommandType = CommandType;
cmd.Connection = ConnectionString;
return cmd;
}
public SqlConnection ConnectionString
{
get
{
_connectionString = new SqlConnection(Getconnect());
_connectionString.Open();
return _connectionString;
}
}
public string Getconnect()
{
string connectionsring = ConfigurationManager.ConnectionStrings["Elearning"].ConnectionString;
return connectionsring;
}
No comments:
Post a Comment