My case is similar to the question asked on this thread - http://stackoverflow.com/questions/23720343/asp-net-change-value-of-textbox-in-grid-on-selected-index-changed?rq=1
In my case i have two gridviews. gridview1 adds row dynamically using button click. i want when i select the dropdownlist in gridview1, it should display a new gridview(gridview2) with record join from different table using sql inner join.Now I want gridview2 to change its respective value whenever i select dropdownlist in gridview1.
Presently, gridview2 displays only record selected from first row dropdownlist. i want it to be able to display different record when i select second row dropdownlist of gridview1.
My Code below:
aspx code:
<asp:GridView ID="SalesGView" runat="server" ShowFooter="True" AutoGenerateColumns="False" Width="609px" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="RowNumber">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<%#Eval("CatName")%>
</ItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="CatCode" AutoPostBack="true" OnSelectedIndexChanged="CatCode_SelectedIndexChanged"
runat="server">
<asp:ListItem Value="0">- All sections</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDesc" runat="server" Text='<%# Eval("Column2")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="QUANTITY">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price">
<ItemTemplate>
<asp:TextBox ID="txtPrice" runat="server" Text='<%# Eval("Column4")%>' CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:TextBox ID="txtAmount" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount(%)">
<ItemTemplate>
<asp:TextBox ID="txtDiscPercent" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount Amount">
<ItemTemplate>
<asp:TextBox ID="txtDiscAmount" runat="server" CssClass="round default-width-input" Width="80"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
<asp:Button ID="Save" runat="server" Text="Save" OnClick="btnSave_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="Label8" runat="server" Text="Label"></asp:Label>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Label8" PopupControlID="pnlpop"
CancelControlID="btnCancel" BehaviorID="mpe" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
<asp:Panel ID="pnlpop" runat="server" BackColor="White" Height="269px" Width="900px" ScrollBars="Both">
<asp:GridView ID="ProdGView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="CatID" HeaderText="CATID" ItemStyle-Width="150" />
<asp:BoundField DataField="PName" HeaderText="Product Name" ItemStyle-Width="100" />
<asp:BoundField DataField="PDesc" HeaderText="Description" ItemStyle-Width="150" />
<asp:BoundField DataField="USP" HeaderText="USP" ItemStyle-Width="150" />
<asp:BoundField DataField="CatName" HeaderText="CAT NAME" ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect" Text="Select" runat="server" OnClick="SelectRow" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</asp:Panel>
Code Behind:
Private Sub SalesGView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles SalesGView.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim ctrl As Control = e.Row.FindControl("CatCode")
If (Not (ctrl) Is Nothing) Then
Dim dd As DropDownList = CType(ctrl, DropDownList)
Dim connStr As String = ConfigurationManager.ConnectionStrings("SY_InventoryConnectionString").ConnectionString
Dim sqlda As SqlDataAdapter = New SqlDataAdapter
Dim com As SqlCommand = New SqlCommand
Dim dt As DataTable
Dim conn As SqlConnection = New SqlConnection(connStr)
dt = New DataTable
com.Connection = conn
com.CommandText = "SELECT CatID, CatName FROM Prod_Category"
sqlda = New SqlDataAdapter(com)
sqlda.Fill(dt)
dd.DataTextField = "CatName"
dd.DataValueField = "CatID"
dd.DataSource = dt
dd.DataBind()
dd.Items.Insert(0, New ListItem("Please select"))
End If
End If
End Sub
Private Sub BindProdGrid()
Dim connStr As String = ConfigurationManager.ConnectionStrings("SY_InventoryConnectionString").ConnectionString
Dim rowIndex As Integer = 0
Dim catname As DropDownList = CType(SalesGView.Rows(rowIndex).Cells(1).FindControl("CatCode"), DropDownList)
Dim sqlda As SqlDataAdapter = New SqlDataAdapter
Dim com As SqlCommand = New SqlCommand
Dim dt As DataTable
Dim conn As SqlConnection = New SqlConnection(connStr)
dt = New DataTable
com.Connection = conn
com.CommandText = ("select Distinct Product.CatID,Product.PName,Product.PDesc, " _
& " Product_Details.USP, Prod_Category.CatName " _
& " from Product inner join Product_Details on Product.CatID= Product_Details.CatID " _
& " inner join Prod_Category on Product_Details.CatID=Prod_Category.CatID where Prod_Category.CatName='" & (catname.SelectedItem.Text) & "' ")
sqlda = New SqlDataAdapter(com)
sqlda.Fill(dt)
ProdGView.DataSource = dt
ProdGView.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
SetInitialRow()
End If
End Sub
Private Sub AddNewRowToGrid()
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
dtCurrentTable.Rows.Clear()
For Each row As GridViewRow In SalesGView.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim box1 As DropDownList = DirectCast(row.FindControl("CatCode"), DropDownList)
Dim box2 As TextBox = DirectCast(row.FindControl("txtDesc"), TextBox)
Dim box3 As TextBox = DirectCast(row.FindControl("txtQty"), TextBox)
Dim box4 As TextBox = DirectCast(row.FindControl("txtPrice"), TextBox)
Dim box5 As TextBox = DirectCast(row.FindControl("txtAmount"), TextBox)
Dim box6 As TextBox = DirectCast(row.FindControl("txtDiscPercent"), TextBox)
Dim box7 As TextBox = DirectCast(row.FindControl("txtDiscAmount"), TextBox)
dtCurrentTable.Rows.Add(box1.SelectedItem.Value, box2.Text, box3.Text, box4.Text, box5.Text, box6.Text, box7.Text)
End If
Next
dtCurrentTable.Rows.Add("", "", "", "", "", "", "")
ViewState("CurrentTable") = dtCurrentTable
Me.SalesGView.DataSource = dtCurrentTable
Me.SalesGView.DataBind()
For i As Integer = 0 To dtCurrentTable.Rows.Count - 1
If Me.SalesGView.Rows(i).RowType = DataControlRowType.DataRow Then
Dim box1 As DropDownList = DirectCast(Me.SalesGView.Rows(i).FindControl("CatCode"), DropDownList)
Dim box2 As TextBox = DirectCast(Me.SalesGView.Rows(i).FindControl("txtDesc"), TextBox)
Dim box3 As TextBox = DirectCast(Me.SalesGView.Rows(i).FindControl("txtQty"), TextBox)
Dim box4 As TextBox = DirectCast(Me.SalesGView.Rows(i).FindControl("txtPrice"), TextBox)
Dim box5 As TextBox = DirectCast(Me.SalesGView.Rows(i).FindControl("txtAmount"), TextBox)
Dim box6 As TextBox = DirectCast(Me.SalesGView.Rows(i).FindControl("txtDiscPercent"), TextBox)
Dim box7 As TextBox = DirectCast(Me.SalesGView.Rows(i).FindControl("txtDiscAmount"), TextBox)
box1.Text = dtCurrentTable.Rows(i)("Column1").ToString()
box2.Text = dtCurrentTable.Rows(i)("Column2").ToString()
box3.Text = dtCurrentTable.Rows(i)("Column3").ToString()
box4.Text = dtCurrentTable.Rows(i)("Column4").ToString()
box5.Text = dtCurrentTable.Rows(i)("Column5").ToString()
box6.Text = dtCurrentTable.Rows(i)("Column6").ToString()
box7.Text = dtCurrentTable.Rows(i)("Column7").ToString()
End If
Next
End Sub
Protected Sub ButtonAdd_Click(sender As Object, e As EventArgs)
AddNewRowToGrid2()
End Sub
Protected Sub SelectRow(sender As Object, e As EventArgs)
Dim dt As New DataTable()
If ViewState("DataTable") Is Nothing Then
dt = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Column2", GetType(String)), New DataColumn("Column4", GetType(String))})
Else
dt = DirectCast(ViewState("DataTable"), DataTable)
End If
Dim row As GridViewRow = TryCast(TryCast(sender, Button).NamingContainer, GridViewRow)
Dim desc As String = row.Cells(2).Text
Dim price As String = row.Cells(3).Text
dt.Rows.Add(desc, price)
Me.SalesGView.DataSource = dt
Me.SalesGView.DataBind()
End Sub