Friday, March 16, 2012

Master and Detail Grid

Hi :)

Can anyone help me out with this code ??

I guess theres something im missing ...to make it work


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
BindMasterGrid()
End If

End Sub

Sub BindMasterGrid()

Dim conNorthwind As SqlConnection
Dim cmdSelect As SqlCommand

conNorthwind = New SqlConnection(ConfigurationSettings.AppSettings("NorthWind"))
cmdSelect = New SqlCommand("Select * From catg", conNorthwind)
conNorthwind.Open()
dgrdCategories.DataSource = cmdSelect.ExecuteReader()
dgrdCategories.DataBind()
conNorthwind.Close()

End Sub

Sub BindDetailGrid(ByVal catg As String)

Dim conNorthwind As SqlConnection
Dim strSelect As String
Dim cmdSelect As SqlCommand

conNorthwind = New SqlConnection(ConfigurationSettings.AppSettings("NorthWind"))
strSelect = "Select id,brand,catg,ref From products Where catg LIKE '%" & catg & "%'"
cmdSelect = New SqlCommand(strSelect, conNorthwind)
cmdSelect.Parameters.Add("@dotnet.itags.org.catg", catg)
Dim adatp As New SqlDataAdapter(cmdSelect)
Dim daSet As New DataSet
adatp.Fill(daSet)
conNorthwind.Open()
dgrdProducts.DataSource = daSet
dgrdProducts.DataBind()
conNorthwind.Close()

End Sub

Sub dgrdCategories_ItemCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)

Dim catg As String
catg = dgrdCategories.DataKeys(e.Item.ItemIndex)
dgrdCategories.SelectedIndex = e.Item.ItemIndex
BindDetailGrid(catg)

End Sub

<HTML>
<HEAD>
<title>DataGridMasterDetail.aspx</title>
</HEAD>
<body>
<form Runat="Server" ID="Form1">
<asp:DataGrid ID="dgrdCategories" OnItemCommand="dgrdCategories_ItemCommand" DataKeyField="id"
AutoGenerateColumns="False" SelectedItemStyle-BackColor="LightGreen" ShowHeader="False" Runat="Server"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3"
Width="124px">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#669999"></SelectedItemStyle>
<ItemStyle ForeColor="#000066"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"></HeaderStyle>
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton Text='<%# Container.DataItem( "catg" ) %>' Runat="Server" ID="Linkbutton1" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<p>
<asp:DataGrid ID="dgrdProducts" HeaderStyle-BackColor="yellow" Runat="Server" BorderColor="White"
BorderStyle="Ridge" BorderWidth="2px" BackColor="White" CellPadding="3" CellSpacing="1" GridLines="None">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
</asp:DataGrid>
</form>
</P>
</body>
</HTML

Thanks in advance

CheersWhat happens when you run it? In what way is it not working?

For some working examples of master/detail grids, take a look at the Reports starter kit, available from the Starter Kits tab on this website.

0 comments:

Post a Comment