Esulin
asked on
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:Im ageButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="tContact_Fname" SortExpression="tContact_F name" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="tContact_Lname" SortExpression="tContact_L name" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="tContact_Office Tel" SortExpression="tContact_O fficeTel" HeaderText="Office Tel."></asp:BoundColumn>
<asp:BoundColumn DataField="tContact_Mobile Tel" SortExpression="tContact_M obileTel" HeaderText="Mobile"></asp: BoundColum n>
<asp:BoundColumn DataField="tContact_EMail" SortExpression="tContact_E Mail" HeaderText="EMail"></asp:B oundColumn >
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<!--<asp:LinkButton runat="server" Text="Edit" CommandName="" CausesValidation="false">< /asp:LinkB utton>-->
<asp:ImageButton id="Imagebutton2" runat="server" CommandName="EditContact" CausesValidation="false"
ToolTip="Click to edit the contact." ImageUrl="BAS/gui/edit.gif "></asp:Im ageButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<!--<asp:LinkButton runat="server" Text="Del" CommandName="" CausesValidation="false">< /asp:LinkB utton>-->
<asp:ImageButton id="btn_deletecontact" runat="server" CommandName="DeleteContact " CausesValidation="false"
ToolTip="Click to delete the contact." ImageUrl="BAS/gui/delete.g if"></asp: ImageButto n>
</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?
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"
<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
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="tContact_Fname"
<asp:BoundColumn DataField="tContact_Lname"
<asp:BoundColumn DataField="tContact_Office
<asp:BoundColumn DataField="tContact_Mobile
<asp:BoundColumn DataField="tContact_EMail"
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<!--<asp:LinkButton runat="server" Text="Edit" CommandName="" CausesValidation="false"><
<asp:ImageButton id="Imagebutton2" runat="server" CommandName="EditContact" CausesValidation="false"
ToolTip="Click to edit the contact." ImageUrl="BAS/gui/edit.gif
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<!--<asp:LinkButton runat="server" Text="Del" CommandName="" CausesValidation="false"><
<asp:ImageButton id="btn_deletecontact" runat="server" CommandName="DeleteContact
ToolTip="Click to delete the contact." ImageUrl="BAS/gui/delete.g
</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?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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