Solved

Set Gridview TemplateField value from a DataTable on Page_Load?

Posted on 2007-04-03
3
543 Views
Last Modified: 2012-06-27
Hey,

I'm using a gridview to display data from a database but I want to do this programmatically and with using templatefields.

I set up the gridview (FileManagerGrid) with the following columns:
                    <Columns>
                        <asp:TemplateField HeaderText="Select">
                            <EditItemTemplate>
                                <asp:CheckBox ID="SelectCheckBox" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="SelectCheckBox" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="File/FolderID" Visible="False">
                            <EditItemTemplate>
                                <asp:Label ID="FileFolderIDLabel" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="FileFolderIDLabel" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="File/Folder">
                            <EditItemTemplate>
                                <asp:TextBox ID="FileFolderNameTextB" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:TextBox ID="FileFolderNameTextB" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="IsFolder" Visible="False">
                            <EditItemTemplate>
                                <asp:CheckBox ID="IsFolderCheckBox" runat="server" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="IsFolderCheckBox" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

I have the values stored in a DataTable which I get on Page_Load but I don't know how to set the values of the templatefield's with their relevant value in the DataTable.
0
Comment
Question by:rebels_mascot
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
Elvio Lujan earned 500 total points
ID: 18843513
dim chk as checkbox = gridview.findcontrol("IsFolderCheckBox")

if..... then
chk.checked=true
end if
0
 
LVL 17

Expert Comment

by:Elvio Lujan
ID: 18843523
do it into a for each...

for each r as gridviewrow in gridview.rows
dim chk as checkbox = r.findcontrol("IsFolderCheckBox")

if..... then
chk.checked=true
end if

next
0
 

Author Comment

by:rebels_mascot
ID: 18844645
Thanks for the quick reply,

I've done the following

        int i = 0;

        foreach (DataRow r in FileFolderTable.Rows)
        {
            FileManagerGrid.
            ((Label)FileManagerGrid.Rows[i].FindControl("FileFolderIDLabel")).Text =
                r["FileFolderID"].ToString();
            ((Label)FileManagerGrid.Rows[i].FindControl("ParentFolderIDLabel")).Text =
                r["ParentFolderID"].ToString();
            ((TextBox)FileManagerGrid.Rows[i].FindControl("FileFolderNameTextB")).Text =
                r["FileFolderName"].ToString();
            ((TextBox)FileManagerGrid.Rows[i].FindControl("FileDescriptionTextB")).Text =
                r["Description"].ToString();
            ((Label)FileManagerGrid.Rows[i].FindControl("FileTypeLabel")).Text =
                r["DateUploaded"].ToString();
            ((Label)FileManagerGrid.Rows[i].FindControl("FileSizeLabel")).Text =
                r["Title"].ToString();
            ((Label)FileManagerGrid.Rows[i].FindControl("DateLabel")).Text =
                r["FileType"].ToString();
            ((CheckBox)FileManagerGrid.Rows[i].FindControl("IsFolderCheckBox")).Checked =
                Boolean.Parse(r["IsFolder"].ToString());

            i++;

in Page_Load, but I need to insert a row first. I'm not sure how to do this? Is there any Gridview.Insert type of function?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now