DataGrid column with a different DataSource

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've got a template column (Status) in my datagrid which must retrieve its content from a different datatable (Status) than the one used by the datagrid (Apps). There is a relationship between the tables and the grid contains the column StatusId which could be used for the link. How can I databind the 'Status' column
 
Could you query this other database, and fill the first dataset with the
statuses, and bind this one dataset?

Another option would be to have a cross-database join using a view.

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com


Paulo said:
I've got a template column (Status) in my datagrid which must retrieve its
content from a different datatable (Status) than the one used by the
datagrid (Apps). There is a relationship between the tables and the grid
contains the column StatusId which could be used for the link. How can I
databind the 'Status' column?
 
Just add your own data binding event handler to the column that you need to get from "elsewhere"; I
often have multiple tables in a dataset and if I need a column from another table that's not the
primary I just create a data binding event hander (with my own parameter, like the datagrid item
index), and using the parameters to that method do a "lookup". If that makes sense... you could also
change the dataset before binding it to the datagrid and create a new column in the data member you
needs (this is probably the fastest method, unless you can change the SQL that generates the
dataset).

Scott

Paulo said:
I've got a template column (Status) in my datagrid which must retrieve its content from a
different datatable (Status) than the one used by the datagrid (Apps). There is a relationship
between the tables and the grid contains the column StatusId which could be used for the link. How
can I databind the 'Status' column?
 
Hey, thanks for the help. I've got my lookup table in my dataset and related to the primary table
Can you give an example of how the event handler would look like
I was trying to use a custom data binding expression but I couldn't figure out a way to get the data I need.
 
If it's in another table in the dataset, you might be better off copying the column into the
primary table (depends I guess); anyway, here's some code that demos calling a separate method to
fillin a column with whatever you want.

Scott

<%@ Import Namespace="System.Data" %>
<%@ Page language="c#" AutoEventWireup="false" %>
<html>
<head>
<title>DGBind</title>
<script language="C#" runat="server">
protected override void OnLoad(EventArgs e)
{
this.DG.DataSource = MyFakeDataSet;
this.DG.DataMember = "Table1";
this.DG.DataBind();
}

private DataSet m_ds;
private DataSet MyFakeDataSet {
get {
if (m_ds != null) {
return m_ds;
}
m_ds = new DataSet();
DataTable t = new DataTable("Table1");
t.Columns.Add("Column1");
m_ds.Tables.Add(t);

for (int i = 0; i < 10; i++) {
DataRow r = m_ds.Tables[0].NewRow();
r["Column1"] = i.ToString();
m_ds.Tables[0].Rows.Add(r);
}
return m_ds;
}
}

protected string GetColumn2(string x) {
return "Get the row in a second table where Column1 = " + x;
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DG" runat="server" autogeneratecolumns="False">
<columns>
<asp:boundcolumn datafield="Column1" headertext="Column1"></asp:boundcolumn>
<asp:templatecolumn headertext="Column2">
<itemtemplate><asp:label runat="server" text='<%# GetColumn2(DataBinder.Eval(Container,
"DataItem.Column1").ToString()) %>'></asp:label>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>


Paulo said:
Hey, thanks for the help. I've got my lookup table in my dataset and related to the primary table.
Can you give an example of how the event handler would look like?
I was trying to use a custom data binding expression but I couldn't figure out a way to get the
data I need.
 
You can just simply dababind it.
And then you can use a function to show your link,the function will be in
the cs file.
The function's param is the simply binded column,and the function's
returning string is what you need in the column.
I don't know if what I said can help you.

Paulo said:
I've got a template column (Status) in my datagrid which must retrieve its
content from a different datatable (Status) than the one used by the
datagrid (Apps). There is a relationship between the tables and the grid
contains the column StatusId which could be used for the link. How can I
databind the 'Status' column?
 
One relatively simple way is to manipulate the datatable after you
retrieve it, before you bind it to the grid.

You can add a Status column to the Apps datatable if it's not there
already. Then iterate through the Apps datatable
and the Status datatable, putting the Status value into the
new column.

Then bind to the datagrid.

Jim
 
Back
Top