我做的是C#b/s程序,页面Gridview绑定数据源datatable。页面代码关键,其中页面设置中列是空的,如下:
<Columns>
 </Columns>
后台页面,datatable列数不定。
gdvParameters.DataSource = datatable;
gdvParameters.DataBind();
可这样,datatable中有几列,页面中gridview列表就有几列,我想加上两列,一列中是复选框,用以勾选后删除该行数据,另一列是可点击的按钮、图片按钮、连接等控件,用以弹出该行数据的修改界面。
说明:(1):不要在aspx页面中加上这两列。因为那样刷新界面后,这两列就没有了。我只想在.cs后台代码中弄好这个。
(2):后添加的两列中要用到控件。
大虾们,给帮帮忙,解决下,可以的话,可以多赠些分!

解决方案 »

  1.   

    添加一行 里面有N列 每个CELL里面包含控件?
      

  2.   

    用不着删除(或者明说:根本不应该删除)。设置GridView.Columns[n].Visible属性吧。
      

  3.   

    第一步:用模版列,把这两列需要的控件放到模版列中;
    第二步:RowDataBound中控制这些列
      

  4.   


    <asp:GridView ID="gridView" runat="server" Width="100%"
                    CellPadding="3"  OnPageIndexChanging ="gridView_PageIndexChanging"
                        BorderWidth="1px" DataKeyNames="编号" OnRowDataBound="gridView_RowDataBound"
                        AutoGenerateColumns="true" PagerStyle-HorizontalAlign="Center" ShowFooter="true" OnRowCreated="gridView_OnRowCreated">
                         <EmptyDataTemplate>
                            <h3>暂无信息!</h3>
                         </EmptyDataTemplate>
                    </asp:GridView>
    protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
            {
                GridView grid = (GridView)sender;
                TableCell cel = new TableCell();
                TableCell cel1 = new TableCell();
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    GridViewRow rowHeader = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Normal);
                    rowHeader.BackColor = System.Drawing.Color.LightBlue;
                    rowHeader.Font.Bold = true;
                    TableCellCollection cells = e.Row.Cells;
                    TableCell headerCell = new TableCell();
                    headerCell = new TableCell();
                    string header = "";
                    //if (ddlType.SelectedValue != "全院")
                        header = ddlType.SelectedValue;
                    if (!string.IsNullOrEmpty(txtBTime.Text) && !string.IsNullOrEmpty(txtETime.Text))
                        header += string.Format("{0}-{1}费用统计报表", Convert.ToDateTime(txtBTime.Text).ToString("yyyy.MM.dd"), Convert.ToDateTime(txtETime.Text).ToString("yyyy.MM.dd"));
                    else if (!string.IsNullOrEmpty(txtBTime.Text) && string.IsNullOrEmpty(txtETime.Text))
                        header += string.Format("{0}-{1}费用统计报表", Convert.ToDateTime(txtBTime.Text).ToString("yyyy.MM.dd"), DateTime.Now.ToString("yyyy.MM.dd"));
                    else
                        header += DateTime.Now.Year + "." + DateTime.Now.Month +"费用统计报表";
                    headerCell.Text = header;
                    hidFileName.Value = header;
                    headerCell.Font.Size = 14;
                    headerCell.ColumnSpan = e.Row.Cells.Count + 2;
                    headerCell.HorizontalAlign = HorizontalAlign.Center;
                    rowHeader.Cells.Add(headerCell);
                    rowHeader.Visible = true;
                    grid.Controls[0].Controls.AddAt(0, rowHeader);
                }
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    cel.Text = "<b>总计</b>";
                    cel1.Text = "<input id='chkAll' type='checkbox'/>";
                }
                else if(e.Row.RowType != DataControlRowType.Footer)
                {
                    cel.Text = "0";
                    CheckBox chk = new CheckBox();
                    chk.ID="chk";
                    cel1.Controls.Add(chk);
                }
                e.Row.Cells.AddAt(0, cel1);
                e.Row.Cells.AddAt(e.Row.Cells.Count, cel);
            }前段时间刚写的  楼主可以参照一下
      

  5.   

    例如datatable中name,code,pwd三个列
    绑定后也就是这三列,我想在后面加上两列,其中一列是复选框控件,另一列是图片按钮控件。
    谁能给出具体的代码。
    我顶,顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶········
      

  6.   

    可以全部在aspx页面中加上那两个列的。刷新后不会消失的。用模板列
      

  7.   

    五楼正解,感谢五楼,感谢大家,感谢csdn。