Reputation:
I have two gridviews in seperate screens.
GridView1
ID Product Date Amount
1 Car1 02/03/2014 $ 15,000
1 Car2 05/03/2014 $ 10,000
2 Bike 01/01/2014 $ 2,500
3 Bus 06/04/2014 $ 25,000
GridView2
ID Product Date Amount
1 Car2 05/03/2014 $ 25,000
2 Bike 01/01/2014 $ 2,500
3 Bus 06/04/2014 $ 25,000
Gridview2 sums up similar ID row values from GridView1 and displays in GridView2 picking the latest effective Date.
Now I am showing my ID column in GridView2 as LinkButton. When I click on value 1 in GridView2 ID column it has to navigate to GridView1 and show only values of ID 1 in the grid.
Code Behind:
GridView1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = DataRepository.GetG1();
GridView1.DataBind();
}
}
public static DataTable GetG1()
{
DataTable dt = new DataTable();
string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strcon))
{
conn.Open();
string strQuery = "Select * from ManLog";
SqlCommand cmd = new SqlCommand(strQuery, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
}
GridView2
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView2.DataSource = DataRepository.GetG2();
GridView2.DataBind();
}
}
public static DataTable GetG2()
{
DataTable dt = new DataTable();
string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strcon))
{
conn.Open();
string strQuery = "Select ID, Product, max(Date),sum(Amount) from ManLog";
SqlCommand cmd = new SqlCommand(strQuery, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
return dt;
}
Link Button for GridView 2:
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="25%" />
<EditItemTemplate>
<asp:TextBox ID="txtID" Width="100%" runat="server" Enabled="false" Text='<%# Eval("ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lblID" runat="server" Text='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
Upvotes: 4
Views: 283
Reputation: 156
I'm not sure what exactly your question is or how your page is laid out, but I think you need to set the url
in the GridView2
link to something like GridView1Page.aspx?id=1
then use that QueryString
to only load the given id. If you don't use the url
or don't want to post, then try a Session
variable.
So in your grid binding method you have something like:
if (Request.QueryString["id"] != null)
{
//load some: select * from table where id = Request.QueryString["id"]
}
else
{
//load all (select * from table)
}
Upvotes: 4
Reputation: 53958
Update your GetG1
method to the following one:
public static DataTable GetG1(int? id=null)
{
DataTable dt = new DataTable();
string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(strcon))
{
conn.Open();
if(id.HasValue)
{
string strQuery = "Select * From ManLog Where Id=@id";
SqlCommand cmd = new SqlCommand(strQuery, conn);
cmd.Parameter.Add(new SqlParameter("id",id.Value);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
else
{
string strQuery = "Select * From ManLog";
SqlCommand cmd = new SqlCommand(strQuery, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
}
return dt;
}
Then as already Joel posted, you should pass an id to the link of your second gridview. For this reason you should update the Page_Load event handler of your first web form, where GridView1 resides.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id;
if(int.TryParse(Request.QueryString["id"], out id)
{
GridView1.DataSource = DataRepository.GetG1(id);
GridView1.DataBind();
}
else
{
GridView1.DataSource = DataRepository.GetG1();
GridView1.DataBind();
}
}
}
Last but not least you should update the corresponding links in GridView2.
<ItemTemplate>
<asp:LinkButton ID="lblEAICode" runat="server" Text='<%# Eval("EAI_Code") %>' />
</ItemTemplate>
At the Text attribute you should place '../../gridview1.aspx?id=<%# Eval("EAI_Code") %>'
, where ../../gridview1.aspx
is the relative path of the web form in which gridview1 resides.
UPDATE
<asp:LinkButton runat="server" CommandArgument='<%# Eval("EAI_Code") %>' OnCommand="LinkButton_Click" Text="View"> </asp:LinkButton>
add the following handler for the click event of your link button in your code behind class
protected void LinkButton_Click(Object sender, CommandEventArgs e)
{
if (e.CommandArgument != null)
{
Response.Redirect("../Product%20Profit.aspx?id=" + e.CommandArgument.ToString());
}
}
Upvotes: 1