开发者

Problem to programmatically add rows to GridView with pre-defined columns

Given a GridView control defined like this:

<asp:GridView
      ID="AttachedFilesGridView"
      runat="server"
      AllowSorting="true"
      AllowPaging="false"
      CellPadding="5"
      AutoGenerateColumns="false"
      CssClass="wsajax_DlgCodeBox">
      <开发者_如何学运维;Columns>
        <asp:TemplateField>
          <ItemTemplate>
            <asp:CheckBox ID="CheckSelect" runat="server" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField Datafield="FilePath" HeaderText="File Path" SortExpression="FilePath" />
        <asp:BoundField Datafield="AttachmentID" HeaderText="Attachment ID" />
       </Columns>
  </asp:GridView>

I am attempting to generate rows for the grid view on the server-side in C# using this code:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("CheckSelect", typeof(CheckBox))); // <<<--- ?????
dt.Columns.Add(new DataColumn("FilePath", typeof(string)));
dt.Columns.Add(new DataColumn("AttachmentID", typeof(string)));

string errMsg = String.Empty;
char[] delimiters = { ',' };
string[] pendingsIDs = PendingFileAttachments.Value.Split(delimiters,     StringSplitOptions.RemoveEmptyEntries);
foreach (string adapterID in pendingsIDs)
{
    FileAdapter adapter = FileAdapter.LoadFileAdapter(profile, new Guid(adapterID), ref errMsg);
    if (adapter != null)
    {
        DataRow dr = dt.NewRow();
        CheckBox cb = new CheckBox();
        cb.Checked = FileAdapter.IsReserved;
        cb.ID = "CheckSelect";
        dr["CheckSelect"] = cb; <<<---?????
        dr["FilePath"] = adapter.GetPartialFilePath();
        dr["AttachmentID"] = adapter.ObjectID.ToString("N");
        dt.Rows.Add(dr);    
    }
}

AttachedFilesGridView.DataSource = dt;
AttachedFilesGridView.DataBind();

The problem is this:

The "FilePath" and "AttachmentID" fields are being set correctly in the grid, however, the CheckBox control is never set correctly; it is always unchecked.

My question:

How do I get this CheckBox field set from C# as I'm creating the new row?

P.S. I am not sure if I am creating the row correctly in the first place.


Using the change antisanity suggested, you need to change the aspx markup as so:

....
      <ItemTemplate>
        <asp:CheckBox ID="CheckSelect" runat="server" Checked='<%# Eval("CheckSelect")' />
      </ItemTemplate>
....


I believe you're going to want to make the column (in the DataTable) type boolean rather than checkbox as it's supposed to be the datasource.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜