I have two gridviews on a form. When the user selects a row from the first gridview the second gridview should populate based on the selected row. The second gridview is not generating as expected. If I remove the "@vendor_id_fk" from the WHERE clause of the query and replace it with a value, the second gridview generates when a row is selected in the first gridview. So I know that's where the issue lies, but I am having some difficulty solving the problem.
Here is the aspx:
<div class="row">
<%--Vendor List--%>
<asp:GridView
ID="gv_vendor_list"
AutoGenerateColumns="false"
AllowPaging="true"
AllowSorting="true"
ShowFooter="true"
runat="server"
DataKeyNames="vendor_id_pk"
Caption='<table width="100%" cellpadding="0" cellspacing="0"><tr><td style="background-color: #04478A;text-align: center;"><h5 style="color: #ffffff; font-weight: bold;">Vendor List</h3></td></tr></table>'
CaptionAlign="Top"
Width="100%"
OnPageIndexChanging="gv_vendor_list_PageIndexChanging"
OnSelectedIndexChanged="gv_vendor_list_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Vendor ID">
<ItemTemplate>
<asp:Label ID="lblVendorId" runat="server" Text='<%# Eval("vendor_id_pk") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor Name">
<ItemTemplate>
<asp:Label ID="lblVendorNm" runat="server" Text='<%# Eval("vendor_nm") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblVendorAdd" runat="server" Text='<%# Eval("vendor_st_add") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblVendorCity" runat="server" Text='<%# Eval("vendor_city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblVendorState" runat="server" Text='<%# Eval("State") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True"></asp:CommandField>
</Columns>
<SelectedRowStyle Font-Bold="True" BackColor="#6666FF" ForeColor="White" />
</asp:GridView>
<%--Document List--%>
<asp:GridView
ID="gv_vendor_doc_list"
AutoGenerateColumns="false"
AllowPaging="true"
PageSize="10"
AllowSorting="true"
ShowHeader="true"
ShowFooter="true"
runat="server"
DataKeyNames="doc_id_pk"
Caption='<table width="100%" cellpadding="0" cellspacing="0"><tr><td style="background-color: #04478A;text-align: center;"><h5 style="color: #ffffff; font-weight: bold;">Document List</h3></td></tr></table>'
CaptionAlign="Top"
Width="100%">
<Columns>
<asp:TemplateField HeaderText="Document ID">
<ItemTemplate>
<asp:Label ID="lblDocId" runat="server" Text='<%# Eval("doc_id_pk") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Document Title">
<ItemTemplate>
<asp:Label ID="lblDocTitle" runat="server" Text='<%# Eval("doc_title") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Document Type">
<ItemTemplate>
<asp:Label ID="lblDocTitle" runat="server" Text='<%# Eval("doc_type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDocTitle" runat="server" Text='<%# Eval("dept") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TWIA / TFPA">
<ItemTemplate>
<asp:Label ID="lblDocTitle" runat="server" Text='<%# Eval("org") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor ID">
<ItemTemplate>
<asp:Label ID="lblVendorIdFk" runat="server" Text='<%# Eval("vendor_id_fk") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="true" />
</Columns>
</asp:GridView>
</div>
Here is the cs:
using System;
using System.Data;
using System.Configuration;
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.Collections;
using System.IO.Compression;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices.AccountManagement;
using System.Net.Mail;
using System.Text;
using System.IO;
using System.Drawing;
namespace idrf
{
public partial class frm_new_doc_rvw_and_sub : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGvVendorList();
}
}
//GridView Vendor List
private void BindGvVendorList()
{
string strConnString = ConfigurationManager.ConnectionStrings["idrfConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("SELECT vendor_id_pk, vendor_nm, vendor_st_dir_list, vendor_st_add, tbl_state_list.state, vendor_city, vendor_state_list, vendor_zip FROM tbl_vendor LEFT JOIN tbl_state_list ON tbl_vendor.vendor_state_list = tbl_state_list.state_id_pk ORDER BY vendor_nm"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gv_vendor_list.DataSource = dt;
this.gv_vendor_list.DataBind();
}
}
}
}
//Populate the Document List Based on Selected Row of Vendor List
protected void gv_vendor_list_SelectedIndexChanged(object sender, EventArgs e)
{
string vfk = gv_vendor_list.SelectedRow.Cells[0].Text;
string strConnString = ConfigurationManager.ConnectionStrings["idrfConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("SELECT td.doc_id_pk, td.doc_title,tdtl.doc_type, tol.org,tdl.dept, td.vendor_id_fk FROM tbl_doc td LEFT JOIN tbl_vendor tv ON td.vendor_id_fk = tv.vendor_id_pk LEFT JOIN tbl_doc_type_list tdtl ON td.doc_type_list = tdtl.doc_type_id_pk LEFT JOIN tbl_org_list tol ON td.doc_org_list = tol.org_id_pk LEFT JOIN tbl_dept_list tdl ON td.doc_dept_list = tdl.dept_id_pk WHERE vendor_id_fk = @vendor_id_fk ", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
cmd.Parameters.AddWithValue("@vendor_id_fk", vfk);
DataTable dt = new DataTable();
sda.Fill(dt);
this.gv_vendor_doc_list.DataSource = dt;
this.gv_vendor_doc_list.DataBind();
}
}
}
}
//Allow Paging for Vendor List GridView
protected void gv_vendor_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGvVendorList();
gv_vendor_list.PageIndex = e.NewPageIndex;
gv_vendor_list.DataBind();
}
}
}
I used the following as a guide: http://www.aspforums.net/Threads/169824/Populate-a-GridView-based-on-the-selected-row-of-another-GridView-in-ASPNet/