将列添加到与SQL数据源关联的gridview [C#/ ASP / Webform]

时间:2016-12-22 09:11:54

标签: c# asp.net gridview

我的asp页面中有一个gridview,数据来自SQL数据源(存储过程),我试图找到一种方法将不在数据源中的列添加到gridview:我想要wether添加一个表示列的asp标记,默认情况下使其不可见,然后以编程方式使其可见或直接在代码中创建新列:

<vol:VlGridView ID="gv" DelayLoadPanelId="up" OnRowCommand="gv_RowCommand" OnRowDataBound="gv_RowDataBound" EnableViewState="true"  runat="server" AutoGenerateColumns="False" AllowSorting="true" AllowPaging="true" OnNew="gv_New" AbcTargetButtonId="btnSearch" AbcTargetFieldId="tbSearch" DataSourceID="sds" OnSorting="gv_Sorting" >
    <columns>
          <asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
    </columns>
</vol:VlGridView>

但是我收到以下错误:

System.Web.HttpException:在所选数据源中找不到“CustomerID”字段

2 个答案:

答案 0 :(得分:0)

最简单的方法是在使用boundfield或自动生成的列时向SQL查询添加虚拟列。

SELECT ColumnA, ColumnB, ColumnC, NULL AS CustomerID FROM table

在这种情况下,CustomerIDNULL,但您也可以指定其他数据类型。

123 AS CustomerID
'stringValue' AS CustomerID

如果您不想/不能更改查询,可以添加TemplateField

<Columns>
    <asp:TemplateField HeaderText="Customer ID">
        <ItemTemplate>
            //stuff goes here
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

答案 1 :(得分:0)

function user_categories() {

wp_add_dashboard_widget(
             'wp_widget_category',         // Widget slug.
             'Categories',         // Title.
             'my_dashboard_category' // Display function.
    );  
function my_dashboard_category() {
     if ( is_user_logged_in() ):
        $current_user = wp_get_current_user();

    if ( ($current_user instanceof WP_User) ) {
        ?>

        <?php
        $args = array(
            'type'                     => 'recipes',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'author'                   => $current_user->ID,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'recipe_categories',
            'pad_counts'               => false );

        $categories = get_categories($args);
        foreach ($categories as $category) {
            $url = get_term_link($category);?>
        <div class="submitted_recipe">
            <a href="<?php echo $url;?>"><?php echo do_shortcode(sprintf('[wp_custom_image_category size="large_blog" term_id="%s"]',$category->term_id)); ?>
                <h2><?php echo $category->name; ?></h2>
        </a>
            </div>
        <?php
        }
        ?>

    <?php
    }
    endif;
    }
}
add_action( 'wp_dashboard_setup', 'user_categories' );