简体   繁体   English

在方法中将数据表绑定到网格视图

[英]Binding Data table to a Grid View in a method

I have a grid view as follows: 我有一个网格视图,如下所示:

<div>
    <asp:Label ID="lblModifiedFilesMessage" runat="server" />
       <asp:GridView ID="gvPopUpModifiedFiles" AutoGenerateColumns="false" CellPadding="0"runat="server">
            <Columns>
            <asp:TemplateField>
            <ItemTemplate>
            <asp:CheckBox ID="chkFileSelect" runat="server" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="FileName" DataField="FileName" />
            </Columns>
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
            </asp:GridView>
            <asp:Button ID="btnOk" Text="OK" runat="server"
            Font-Bold="true" onclick="btnOk_Click" /><br />
    </div>

I am trying to bind the data for this grid view in a method as follows: 我正在尝试通过以下方法将此网格视图的数据绑定:

private void PopUpModifiedFiles(List<ProjectFile> ModifiedFiles)
{
    this.lblModifiedFilesMessage.Text = "Below files are modified in the Source Server and will be archived with latest version. Please select any files if they are to be retained with older version in archive";
    DataTable dtModifiedFiles = new DataTable();
    dtModifiedFiles.Columns.Add("FileName");
    foreach (ProjectFile modifiedFile in ModifiedFiles)
    {
        DataRow drFileName = dtModifiedFiles.NewRow();
        drFileName["FileName"] = modifiedFile.FileName;
        dtModifiedFiles.Rows.Add(drFileName);
    }
    gvPopUpModifiedFiles.DataSource = dtModifiedFiles;
    gvPopUpModifiedFiles.DataBind();

}

Here while assigning the data table to the grid view It is showing me an error as does not contain definition for gvPopUpModifiedFiles . 在这里,将数据表分配给网格视图时,这向我显示了一个错误,因为其中不包含gvPopUpModifiedFiles定义。 Do I need to write any method for this gvPopUpModifiedFiles? 我需要为此gvPopUpModifiedFiles写任何方法吗?

How to assign the values? 如何分配值?

For bind the grid you must set DataTable on the property DataSource and call the method DataBind 为了绑定网格,必须在属性DataSource上设置DataTable并调用方法DataBind

gvPopUpModifiedFiles.DataSource = dtModifiedFiles;
gvPopUpModifiedFiles.DataBind();

The problem could be the attribute on directive Page, properties CodeBehind Inherits. 问题可能出在伪指令Page的属性CodeBehind Inherits。 You can control in this way 您可以通过这种方式进行控制

File aspx: 文件aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication4.Index" %>

File CS: 文件CS:

namespace WebApplication4
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

The file Index.aspx.cs in CodeBehind must exists and Inherits attribute must be the fullname's class in CS CodeBehind中的Index.aspx.cs文件必须存在,并且Inherits属性必须是CS中全名的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM