Solved

Set Gridview TemplateField value from a DataTable on Page_Load?

Posted on 2007-04-03
3
542 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:
lem2802 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:lem2802
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

18 Experts available now in Live!

Get 1:1 Help Now