473,382 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

datagrid with 2 data sources

I am binding data to a datagrid, but there is 1 field that I need to use
a separate query to populate.

This is the field :

<asp:TemplateColumn HeaderText="SIP Account/s" ItemStyle-Width="75px"
HeaderStyle-HorizontalAlign=Center ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:DropDownList id="ddlSIPAccounts" runat="server"
DataValueField="SIP Account" Font-Name="arial" Font-Size="8pt"
DataTextField="SIP Account"
DataSource='<%# GetSIPAccounts() %>' />
</ItemTemplate>
</asp:TemplateColumn>

But using GetSIPAccounts, I somehow need to only populate each row with
the relevant SIP Accounts for that row - the row identifier is the
Product ID and I need to only show the SIP Accounts for that Product ID
for each row.

This is my GetSIPAccounts procedure :

public DataSet GetSIPAccounts()
{
shopping_cartDB scCart = new shopping_cartDB();
string strCartID = scCart.GetShoppingCartId();

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
string strSIPAccounts = "SELECT SIPACCOUNT AS 'SIP Account' FROM
SHOPPING_CART_SIP WHERE CARTID = '" + strCartID + "' AND PRODUCTID = " +
Convert.ToInt32(dgShoppingCart.Items[correct item here].Cells[0].Text);

SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSIPAccounts,
objConnection);

DataSet ddlDataSet = new DataSet();

objDataAdapter.Fill(ddlDataSet, "SIPAccounts");

return ddlDataSet;

}

Can anybody help me out with this? Any help would be really
appreciated!
Cheers,

Mike

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
7 1657
Mike P wrote:
I am binding data to a datagrid, but there is 1 field that I need to use
a separate query to populate.

This is the field :

<asp:TemplateColumn HeaderText="SIP Account/s" ItemStyle-Width="75px"
HeaderStyle-HorizontalAlign=Center ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:DropDownList id="ddlSIPAccounts" runat="server"
DataValueField="SIP Account" Font-Name="arial" Font-Size="8pt"
DataTextField="SIP Account"
DataSource='<%# GetSIPAccounts() %>' />
</ItemTemplate>
</asp:TemplateColumn>

But using GetSIPAccounts, I somehow need to only populate each row with
the relevant SIP Accounts for that row - the row identifier is the
Product ID and I need to only show the SIP Accounts for that Product ID
for each row.

This is my GetSIPAccounts procedure :

public DataSet GetSIPAccounts()
{
shopping_cartDB scCart = new shopping_cartDB();
string strCartID = scCart.GetShoppingCartId();

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
string strSIPAccounts = "SELECT SIPACCOUNT AS 'SIP Account' FROM
SHOPPING_CART_SIP WHERE CARTID = '" + strCartID + "' AND PRODUCTID = " +
Convert.ToInt32(dgShoppingCart.Items[correct item here].Cells[0].Text);

SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSIPAccounts,
objConnection);

DataSet ddlDataSet = new DataSet();

objDataAdapter.Fill(ddlDataSet, "SIPAccounts");

return ddlDataSet;

}

Can anybody help me out with this? Any help would be really
appreciated!
Cheers,

Mike

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!


To get the product ID into your method, you should pass in an arg to
your method from the binding statement; pass in the current row's
product id out of that DataSet....Container.DataItem gives you the
current DataRow being bound to the current datagrid item, so you could
do something like:

DataSource='<%#
GetSIPAccounts(((DataRow)Container.DataItem)["prod_id_col_name"]) %>'

note you'll need to add an Import directive for System.Data to your
..aspx page since I used DataRow in a cast there. You could then do
another cast if you want, because as this is, the arg will be an Object

public DataSet GetSIPAccounts(Object productID)
{
//cast the prod id appropriately before using
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #2
I think you'll need something like this, you'll also need to modify the GetSipAccounts function to take in the ProductID as a parameter. You should be able to do it from there.

<asp:DropDownList id="ddlSIPAccounts" runat="server"
DataValueField="SIP Account" Font-Name="arial" Font-Size="8pt"
DataTextField="SIP Account"
DataSource='<%# GetSIPAccounts(Container.DataItem("Product ID")) %>' />

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
I am binding data to a datagrid, but there is 1 field that I need to use
a separate query to populate.

This is the field :

<asp:TemplateColumn HeaderText="SIP Account/s" ItemStyle-Width="75px"
HeaderStyle-HorizontalAlign=Center ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:DropDownList id="ddlSIPAccounts" runat="server"
DataValueField="SIP Account" Font-Name="arial" Font-Size="8pt"
DataTextField="SIP Account"
DataSource='<%# GetSIPAccounts() %>' />
</ItemTemplate>
</asp:TemplateColumn>

But using GetSIPAccounts, I somehow need to only populate each row with
the relevant SIP Accounts for that row - the row identifier is the
Product ID and I need to only show the SIP Accounts for that Product ID
for each row.

This is my GetSIPAccounts procedure :

public DataSet GetSIPAccounts()
{
shopping_cartDB scCart = new shopping_cartDB();
string strCartID = scCart.GetShoppingCartId();

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
string strSIPAccounts = "SELECT SIPACCOUNT AS 'SIP Account' FROM
SHOPPING_CART_SIP WHERE CARTID = '" + strCartID + "' AND PRODUCTID = " +
Convert.ToInt32(dgShoppingCart.Items[correct item here].Cells[0].Text);

SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSIPAccounts,
objConnection);

DataSet ddlDataSet = new DataSet();

objDataAdapter.Fill(ddlDataSet, "SIPAccounts");

return ddlDataSet;

}

Can anybody help me out with this? Any help would be really
appreciated!


Cheers,

Mike



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #3
Craig, Raterus,

I've tried what you have suggested, but I get the error 'denotes a
'property' where a 'method' was expected'. Here is my complete datagrid
:

<asp:datagrid id="dgShoppingCart" runat="server" CellPadding="3"
Font-Name="arial" Font-Size="8pt" ForeColor="Black"
HeaderStyle-ForeColor="#FFFFFF"
HeaderStyle-BackColor="#000000" HeaderStyle-Font-Bold="True"
GridLines=Horizontal
BorderColor="DarkBlue" BackColor="LightGoldenrodYellow"
AutoGenerateColumns="False" DataKeyField="Quantity">
<ItemStyle BackColor="White"></ItemStyle>
<SelectedItemStyle ForeColor="GhostWhite"
BackColor="DarkSlateBlue"></SelectedItemStyle>
<AlternatingItemStyle
BackColor="#eeeeee"></AlternatingItemStyle>
<HeaderStyle Font-Bold=True BackColor=#000000
ForeColor=#FFFFFF></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Product*ID"
ItemStyle-Width="40px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblProductID" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Product ID") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Product Type"
ItemStyle-Width="140px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblProductType" font-bold="True"
Font-Name="tahoma" ForeColor="Maroon" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Product Type") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SIP Account/s"
ItemStyle-Width="75px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:DropDownList id="ddlSIPAccounts" runat="server"
DataValueField="SIP Account" Font-Name="arial" Font-Size="8pt"
DataTextField="SIP Account"
DataSource='<%# GetSIPAccounts(Container.DataItem("Product ID")) %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SIP Domain"
ItemStyle-Width="75px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblSIPDomain" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "SIP Domain") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Unit Price (GBP)"
HeaderText="Unit Price (GBP)" DataFormatString="{0:c}"
ItemStyle-Width="50px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center />
<asp:TemplateColumn HeaderText="Quantity"
ItemStyle-Width="55px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblQuantity" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Quantity") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sub Total (GBP)"
ItemStyle-Width="60px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblSubTotal" runat="server"
ForeColor="Maroon" Text='<%# DataBinder.Eval(Container.DataItem, "Sub
Total", "{0:c}") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Remove"
ItemStyle-Width="50px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:CheckBox id="chkRemove" runat="server"
Checked=False EnableViewState=True />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
I am populating the datagrid with a stored procedure, and I am trying to
add the additional column with this code :

public DataSet GetSIPAccounts(int intProductID)
{
shopping_cartDB scCart = new shopping_cartDB();
string strCartID = scCart.GetShoppingCartId();

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
string strSIPAccounts = "SELECT SIPACCOUNT AS 'SIP Account' FROM
SHOPPING_CART_SIP WHERE CARTID = '" + strCartID + "' AND PRODUCTID = " +
intProductID;

SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSIPAccounts,
objConnection);

DataSet ddlDataSet = new DataSet();

objDataAdapter.Fill(ddlDataSet, "SIPAccounts");

return ddlDataSet;

}

Thanks,

Mike


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4
sorry, I gave you the VB answer, you need the C# one

DataSource='<%# GetSIPAccounts(Container.DataItem["Product ID"]) %>

note the [ ] around "Product ID" now and not ( )

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message news:ug**************@tk2msftngp13.phx.gbl...
Craig, Raterus,

I've tried what you have suggested, but I get the error 'denotes a
'property' where a 'method' was expected'. Here is my complete datagrid
:

<asp:datagrid id="dgShoppingCart" runat="server" CellPadding="3"
Font-Name="arial" Font-Size="8pt" ForeColor="Black"
HeaderStyle-ForeColor="#FFFFFF"
HeaderStyle-BackColor="#000000" HeaderStyle-Font-Bold="True"
GridLines=Horizontal
BorderColor="DarkBlue" BackColor="LightGoldenrodYellow"
AutoGenerateColumns="False" DataKeyField="Quantity">
<ItemStyle BackColor="White"></ItemStyle>
<SelectedItemStyle ForeColor="GhostWhite"
BackColor="DarkSlateBlue"></SelectedItemStyle>
<AlternatingItemStyle
BackColor="#eeeeee"></AlternatingItemStyle>
<HeaderStyle Font-Bold=True BackColor=#000000
ForeColor=#FFFFFF></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Product ID"
ItemStyle-Width="40px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblProductID" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Product ID") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Product Type"
ItemStyle-Width="140px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblProductType" font-bold="True"
Font-Name="tahoma" ForeColor="Maroon" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Product Type") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SIP Account/s"
ItemStyle-Width="75px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:DropDownList id="ddlSIPAccounts" runat="server"
DataValueField="SIP Account" Font-Name="arial" Font-Size="8pt"
DataTextField="SIP Account"
DataSource='<%# GetSIPAccounts(Container.DataItem("Product ID")) %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="SIP Domain"
ItemStyle-Width="75px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblSIPDomain" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "SIP Domain") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Unit Price (GBP)"
HeaderText="Unit Price (GBP)" DataFormatString="{0:c}"
ItemStyle-Width="50px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center />
<asp:TemplateColumn HeaderText="Quantity"
ItemStyle-Width="55px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblQuantity" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Quantity") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Sub Total (GBP)"
ItemStyle-Width="60px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:Label id="lblSubTotal" runat="server"
ForeColor="Maroon" Text='<%# DataBinder.Eval(Container.DataItem, "Sub
Total", "{0:c}") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Remove"
ItemStyle-Width="50px" HeaderStyle-HorizontalAlign=Center
ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:CheckBox id="chkRemove" runat="server"
Checked=False EnableViewState=True />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>


I am populating the datagrid with a stored procedure, and I am trying to
add the additional column with this code :

public DataSet GetSIPAccounts(int intProductID)
{
shopping_cartDB scCart = new shopping_cartDB();
string strCartID = scCart.GetShoppingCartId();

SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
string strSIPAccounts = "SELECT SIPACCOUNT AS 'SIP Account' FROM
SHOPPING_CART_SIP WHERE CARTID = '" + strCartID + "' AND PRODUCTID = " +
intProductID;

SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSIPAccounts,
objConnection);

DataSet ddlDataSet = new DataSet();

objDataAdapter.Fill(ddlDataSet, "SIPAccounts");

return ddlDataSet;

}

Thanks,

Mike




*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #5
Can you actually get this working in your applications? It seems that
it won't accept GetSIPAccounts(Container.DataItem("Product ID")) or
GetSIPAccounts(Container.DataItem["Product ID"]). When I try
GetSIPAccounts(16) the value 16 gets passed, but I can't get it to pass
anything related to Container.DataItem.
Thanks,

Mike

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6


The solutions proposed might be the quickest to implement,
given the code you have now, but an alternative method
would be:

If you have two data sources, create a new datatable that
will eventually be the datasource for the grid (you might
clone one of the original data sources, and then add columns as needed.)
Iterate through your two original datasources and populate the new
datatable, then bind it.

Or more simply, add a column to your datasource, iterate through it and
call a function to populate the new column for each row,
then bind it to the grid.

HTH,
Jim

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #7
Yes, this works fine for me (vb.net)

in a repeater control's <itemtemplate>

<asp:dropdownlist id="ddlDDL" runat="server" datasource='<%# getListItems(cint(Container.DataItem("docID"))) %>'></asp:dropdownlist>

in codebehind:

Protected Function getListItems(ByVal docID As Integer) As String()
Dim retVal(10) As String

Dim i As Integer
For i = 0 To 10
retVal(i) = CStr(docID + i)
Next

Return retVal
End Function

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message news:Oo**************@tk2msftngp13.phx.gbl...
Can you actually get this working in your applications? It seems that
it won't accept GetSIPAccounts(Container.DataItem("Product ID")) or
GetSIPAccounts(Container.DataItem["Product ID"]). When I try
GetSIPAccounts(16) the value 16 gets passed, but I can't get it to pass
anything related to Container.DataItem.


Thanks,

Mike



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Schlauberger | last post by:
I have a form that has a Datagrid, there is a menu option that I use to toggle the Datagrid between two different data sources (x and y). As I continue to toggle the datagrid is duplication each...
10
by: Robert Schuldenfrei | last post by:
Hi NG: I have the following code working. The GetProdStrtList() returns a DataTable called psTable. A Grid control is successfully bound to this table and displays correctly. I would like to...
4
by: Steve B. | last post by:
I have a DataGrid on the left and TextBoxes (TB) on the right. The TB's reflect the contents of the grid cells. Sorting of columns (both thru VS and programmatically) work fine except, when the...
7
by: TT (Tom Tempelaere) | last post by:
Hi there, I'm struggling to get the right event. I want to be notified of a change in a cell in a DataGrid made by a user. I need following information about the change: * the row index * the...
6
by: Alpha | last post by:
I have several textboxes that I need to chang the text when the selection row is changed in a datagrid. I have the following code. This textbox displayes the initial selection but when I click on...
5
by: Allen | last post by:
Is there a way to bind a datagrid in VB.Net to an ADODB Recordset object? I have tried: rs.Open ("SELECT Name FROM tblStaff",connection) dgTest.SetDataBindings(rs,"") The recordset is...
1
by: Will Chamberlain | last post by:
I just created an application that displays data from 2 sources in a datagrid. There are 2 repeaters nested in the datagrid (probably not the best idea). The whole purpose of this application is to...
10
by: Nick | last post by:
Hello, Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid where I want to let the user delete columns. I added a context menu to the datagrid that has a delete option....
6
by: SatishPasala | last post by:
Hi I tried all the things I know am out of ideas now. Can you guys help me out with this? I have 4 grids on a page with I load on the Page_Load event. One of the grids has paging. When I move...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.