dynamically binding a field in a Gridview

Discussion in 'ASP .Net' started by Robert Smith, Mar 8, 2008.

  1. Robert Smith

    Robert Smith Guest

    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
     
    Robert Smith, Mar 8, 2008
    #1
    1. Advertising

  2. 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";


    --
    Misbah Arefin
    https://mcp.support.microsoft.com/profile/MISBAH.AREFIN
    http://www.linkedin.com/in/misbaharefin


    "Robert Smith" wrote:

    > 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
     
    Misbah Arefin, Mar 9, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amit
    Replies:
    6
    Views:
    13,808
    Assimalyst
    Oct 24, 2006
  2. sanju
    Replies:
    1
    Views:
    6,156
    Islamegy®
    Aug 6, 2006
  3. gnewsgroup
    Replies:
    1
    Views:
    2,298
    =?Utf-8?B?SklHTkVTSA==?=
    Oct 12, 2007
  4. Sound
    Replies:
    2
    Views:
    453
    Randy Webb
    Sep 28, 2006
  5. jr
    Replies:
    3
    Views:
    426
Loading...

Share This Page