Changing the DataField of a DataGrid at runtime

Hey all,

It's been a while since I did any programming in VB/ASP.NET so don't shoot me if this is obvious.

I have a datagrid control on a page that I populate when the page is loaded. I'm changing the functionality of the page so that based on a certain variable the DataGrid will display different parts of the data (i.e. depending on which user is viewing the data).

I currently deffine the Datagrid in the aspx file as follows:
<TD colSpan="2"><asp:datagrid id="dgdContactsList" runat="server" BorderWidth="1px" BorderColor="#CCCCCC" AllowPaging="True"
            DataKeyField="tContact_id" AutoGenerateColumns="False" PageSize="15">
            <Columns>
                  <asp:TemplateColumn>
                        <ItemTemplate>
                              <asp:ImageButton id="Imagebutton1" runat="server" CommandName="ViewContact" CausesValidation="false"
                                    ToolTip="Click to view the contact." ImageUrl="BAS/gui/view.gif"></asp:ImageButton>
                        </ItemTemplate>
                  </asp:TemplateColumn>
                  <asp:BoundColumn DataField="tContact_Fname" SortExpression="tContact_Fname" HeaderText="First Name"></asp:BoundColumn>
                  <asp:BoundColumn DataField="tContact_Lname" SortExpression="tContact_Lname" HeaderText="Last Name"></asp:BoundColumn>
                  <asp:BoundColumn DataField="tContact_OfficeTel" SortExpression="tContact_OfficeTel" HeaderText="Office Tel."></asp:BoundColumn>
                  <asp:BoundColumn DataField="tContact_MobileTel" SortExpression="tContact_MobileTel" HeaderText="Mobile"></asp:BoundColumn>
                  <asp:BoundColumn DataField="tContact_EMail" SortExpression="tContact_EMail" HeaderText="EMail"></asp:BoundColumn>
                  <asp:TemplateColumn Visible="False">
                        <ItemTemplate>
                              <!--<asp:LinkButton runat="server" Text="Edit" CommandName="" CausesValidation="false"></asp:LinkButton>-->
                              <asp:ImageButton id="Imagebutton2" runat="server" CommandName="EditContact" CausesValidation="false"
                                    ToolTip="Click to edit the contact." ImageUrl="BAS/gui/edit.gif"></asp:ImageButton>
                        </ItemTemplate>
                  </asp:TemplateColumn>
                  <asp:TemplateColumn Visible="False">
                        <ItemTemplate>
                              <!--<asp:LinkButton runat="server" Text="Del" CommandName="" CausesValidation="false"></asp:LinkButton>-->
                              <asp:ImageButton id="btn_deletecontact" runat="server" CommandName="DeleteContact" CausesValidation="false"
                                    ToolTip="Click to delete the contact." ImageUrl="BAS/gui/delete.gif"></asp:ImageButton>
                        </ItemTemplate>
                  </asp:TemplateColumn>
            </Columns>


So, what i need to do now is write a function that would allow me to change the values of the datafield for the boundcolumns onload of the page. I guess I don't mind creating the boundcolumns as part of that function rather than predefining them in the aspx.

Suggestions?
EsulinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nauman_ahmedCommented:
Do it in ItemDataBound event:

private sub dgdContactsList_ItemDatabound(source as object, e as DataGridItemEventArgs) Handles dgdContactsList.ItemDataBound
 if e.Item.ItemType = ListItemType.Item or e.Item.ItemType = ListItemType.AlternatingItem then
   'Do your evaluation and set the column value
   e.Item.Cells(CELL_INDEX).Text = DataBinder.Eval(e.Item.DataItem,"COLUMN_NAME")
 end if
 
End sub

-Nauman.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deanvanrooyenCommented:
if you need to update a database with the chnage data then I rather predeifned the columns and do the whole process of setting the row to edit mode then using sql to update the db and rebind the grid
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.