dynamically binding a field in a Gridview

R

Robert Smith

Hi,
I would like to bind a datafield in gridvew dynamilcally because I want
to use the same grid for 2 different datasets. The column names in the
dataset are the same for both.
In dataset 1 I have a column called AgencyName,
in dataset 2 I have a column name called AdvertiserName both in the 3rd column
Can I bind the field to a particular column in the dataset
I have

<asp:TemplateField HeaderText="Agency_Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("Advertiser_Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

Can the Bind command be manipulated to use dataset.table.column[3] etc, if
so how?

Thanks in advance
Robert
 
M

Misbah Arefin

you cannot specify an ordinal in the eval/bind functions
the bind method takes the name of a data field and returns a string
containing the value of that field from the current record in the data source

public static Object Eval(
Object container,
string expression
)
container:
the object reference against which the expression is evaluated. this must be
a valid object identifier in the page's specified language.

expression:
the navigation path from the container object to the public property value
to be placed in the bound control property. this must be a string of property
or field names separated by periods, such as Tables[0].DefaultView.[0].Price

you could change the column name for your dataset2 to match the column name
of dataset1 before binding to the gridview control e.g.
DataSet2.Table[0].Columns[3].ColumnName =
DataSet1.Table[0].Columns[3].ColumnName
//bind to data source
DataGrid1.Bind();
//revert the change in dataset if the columnname is needed afterwards
DataSet2.Table[0].Columns[3].ColumnName = "OrgColName";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top