Gridview and Detail view

Dec 15 2010 12:10 PM
Hi,

I have two table
Table 1: job_details
Job_id
Project Name

Table 2: chap_details
chap_id
Job_id
chap_no
ms_pages

In the aspx page, I have grid view and details view.

My problem is
In the gridview, template fields called job_id, chapter_no
and view button

whenever i select the chapter of particular job and click the view button.
It displays the ms pages of the particular job and chapter.

Note: A job can have multiple chapters

But in the following program works when i select the first value value in the dropdown chapter list box.

Plz help me.

.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 605px">
<asp:GridView ID="grvTask" CssClass="gvHeader" runat="server" AutoGenerateColumns="False"
RowStyle-HorizontalAlign="Center"
GridLines="None" PagerStyle-Width="5%" HeaderStyle-BackColor="#72a1c1" HeaderStyle-ForeColor="white"
PagerStyle-Font-Bold="true" PagerStyle-HorizontalAlign="Center" PagerStyle-BackColor="linen" PageSize="5" OnSelectedIndexChanged="grvTask_SelectedIndexChanged" CellPadding="4" ForeColor="#333333" >

<Columns>

<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="2%" HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Job ID">
<ItemTemplate>
<asp:Label ID="lblJob_no" runat="server" Text='<%#Eval("Job_id") %>' />
<asp:HiddenField ID="hdnJobNo" runat="server" Value='<%#Eval("Job_id") %>' />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Chapters">
<ItemTemplate>
<asp:DropDownList ID="ddChapter" runat="server" AutoPostBack="false" Width="120px" ></asp:DropDownList>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%"/>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnView" runat="server" Text="View" OnClick="btnView_Click" />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="15%" />
</asp:TemplateField>

</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" Width="5%" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>


<div id="secondDiv" runat="server">
<table>
<tr>
<td style="width: 283px">
<asp:GridView ID="grvDetails" CssClass="gvHeader" runat="server" AutoGenerateColumns="false"
RowStyle-HorizontalAlign="Center"
GridLines="Horizontal" PagerStyle-Width="5%" HeaderStyle-BackColor="#72a1c1" HeaderStyle-ForeColor="white"
PagerStyle-Font-Bold="true" PagerStyle-HorizontalAlign="Center" PagerStyle-BackColor="linen"
AllowPaging="false" PageSize="5" Width="611px">

<Columns>

<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="2%" HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Job ID">
<ItemTemplate>
<asp:Label ID="lblJob_no" runat="server" Text='<%#Eval("Job_id") %>' />
<asp:HiddenField ID="hdnJobNo" runat="server" Value='<%#Eval("Job_id") %>' />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Chapters">
<ItemTemplate>
<asp:Label ID="lblChapter" runat="server" Text='<%#Eval("chapter_no") %>' />
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="5%" />
</asp:TemplateField>

<asp:TemplateField HeaderText="MS Pages">
<ItemTemplate>
<asp:Label ID="lblReworkCount" runat="server" Text='<%#Eval("ms_pages") %>' />

<%--<asp:Button ID="btnView" runat="server" Text="View" OnClick="btnView_Click" />--%>
</ItemTemplate>
<HeaderStyle CssClass="columnHeading" Width="15%" />
</asp:TemplateField>

</Columns>
<RowStyle HorizontalAlign="Center" />
<PagerStyle BackColor="Linen" Font-Bold="True" HorizontalAlign="Center" Width="5%" />
<HeaderStyle BackColor="#72A1C1" ForeColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>

</form>
</body>
</html>


C#:

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;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection sqlCon = new SqlConnection();
SqlCommand sqlCmd = new SqlCommand();
string connectionString;

protected void Page_Load(object sender, EventArgs e)
{
OpenConnection();

if (!IsPostBack)
{
fnFillGrid();
fillDdlChapter();
secondDiv.Visible = false;
}

}
public void OpenConnection()
{
connectionString = System.Configuration.ConfigurationSettings.AppSettings["myconn"];
sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
}

public void fnFillGrid()
{
DataSet ds = new DataSet();
sqlCmd = new SqlCommand("select distinct(job_id) from job_details order by job_id", sqlCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlCmd;
da.Fill(ds);
grvTask.DataSource = ds;
grvTask.DataBind();
}

public void fillDdlChapter()
{
for (int i = 0; i < grvTask.Rows.Count; i++)
{
Label lblJob_no = ((Label)(grvTask.Rows[i].Cells[2].Controls[1]).FindControl("lblJob_no"));
DropDownList ddlchapter = ((DropDownList)(grvTask.Rows[i].Cells[2].Controls[1]).FindControl("ddChapter"));

DataSet ds = new DataSet();
sqlCmd = new SqlCommand("select distinct(chapter_no),job_id from chap_details where job_id ='" + lblJob_no.Text + "' ", sqlCon);
SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = sqlCmd;
da.Fill(ds);

ddlchapter.DataSource = ds;
ddlchapter.DataValueField = "job_id";
ddlchapter.DataTextField = "chapter_no";
ddlchapter.DataBind();
}
}

protected void btnView_Click(object sender, EventArgs e)
{
string jobno = Convert.ToString(((HiddenField)(((Button)sender).Parent.FindControl("hdnJobNo"))).Value);
string chapter = Convert.ToString(((DropDownList)(((Button)sender).Parent.FindControl("ddChapter"))).SelectedItem);

secondDiv.Visible = true;
DataSet ds = new DataSet();
sqlCmd = new SqlCommand("select job_id,chapter_no,ms_pages from chap_details where job_id='" + jobno + "' and chapter_no='" + chapter + "'", sqlCon);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = sqlCmd;
da.Fill(ds);
grvDetails.DataSource = ds;
grvDetails.DataBind();
}

}

Problem is: when i am selecting the second item in the dropdown box of a particular job means it shoes the first item value in the details view.

bye
Murugavel S

Answers (4)