使用autogeneratedcolumns选项自定义gridview '从主数据表绑定数据

本文关键字:数据表 绑定 数据 选项 autogeneratedcolumns 自定义 gridview 使用 | 更新日期: 2023-09-27 18:06:01

我已经在这里问过这个问题了:

但是我对这个还有几个问题。

1。我有一个数据表,其中每一行都有保险名称,植物类型,保费...."等数据

在我的前端,我需要显示如下:

Insurance Name     HealthNet       Harvard         UniCare
Plan Type            HMO            PPO              HMO  
Premium              100            150             200 

为此,我使用以下方法来透视数据表并将其分配给gridview)

private DataTable PivotTable(DataTable origTable){
        DataTable newTable = new DataTable();
        DataRow dr = null;
        //Add Columns to new Table
        for (int i = 0; i <= origTable.Rows.Count; i++)
        {
            newTable.Columns.Add(new DataColumn(origTable.Columns[i].ColumnName, typeof(String)));
        }
        //Execute the Pivot Method
        for (int cols = 0; cols < origTable.Columns.Count; cols++)
        {
            dr = newTable.NewRow();
            for (int rows = 0; rows < origTable.Rows.Count; rows++)
            {
                if (rows < origTable.Columns.Count)
                {
                    dr[0] = origTable.Columns[cols].ColumnName; // Add the Column Name in the first Column
                    dr[rows + 1] = origTable.Rows[rows][cols];
                }
            }
            newTable.Rows.Add(dr); //add the DataRow to the new Table rows collection
        }
        return newTable;
}

和数据绑定如下:

DataTable tempTable = PivotTable(actualDataTable);
myGridView.DataSource = tempTable;
myGridView.DataBind();

现在我的输出看起来像:

Insurance Name     HealthNet       Harvard         UniCare
Plan Type            HMO            PPO              HMO  
Premium              100            150             200 

但是现在我的网格视图显示了来自数据源的所有值。我想自定义我的网格视图外观,这样我就可以插入图像到每个保险名称旁边的表行,并隐藏一些行也。

注意:我不仅限于gridview。我对任何控制都很好,但是,数据应该是动态的,而不是硬编码。

感谢编辑:

我是否可以根据列数而不是下面的循环来显示每个保险名称旁边的图像?

protected void myGridview_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = "<img src=../images/" + e.Row.Cells[1].Text + ".png />";
                e.Row.Cells[2].Text = "<img src=../images/" + e.Row.Cells[2].Text + ".png />";
            }
    }

使用autogeneratedcolumns选项自定义gridview '从主数据表绑定数据

可以使用GridView的RowDataBound事件

在RowDataBound事件中,您可以操作每个单元格的数据。例如,在单元格中插入图像。

但是,不能隐藏整行。如果不想显示某一行,则需要在透视表方法中实现该逻辑。