我可以在GridView,ASP.NET中绘制另一页的行

发布于 2025-02-13 02:43:30 字数 775 浏览 0 评论 0原文

我无法解决一件事,我想根据一列绘制GridView的一些行。我对此没有任何问题,但是当我更改页面时,我无法绘画。

这就是我进行分页的方式

    GridView2.PageIndex =  e.NewPageIndex;
        loadgrid();
        paintpagin(e.NewPageIndex);

,因此我尝试将其绘制为

GridView2.PageIndex = NewPageIndex;

        foreach (GridViewRow Rowe in GridView2.Rows)
        {
                CheckBox Rc = (CheckBox)Rowe.FindControl("rdaprobar");
                RadioButton Ri = (RadioButton)Rowe.FindControl("rdpendiente");
                RadioButton Rd = (RadioButton)Rowe.FindControl("rdCandelar");

                if (Rc.Checked == true)
                {
                    GridView2.Rows[Rowe.DataItemIndex].BackColor = ColorTranslator.FromHtml("#bcf5be");
                }
        }

I can't solve one thing, I want to paint some rows of the GridView depending on a column. I have no problems with this, but when I change the page, I can't get it to paint.

This is how I do the pagination

    GridView2.PageIndex =  e.NewPageIndex;
        loadgrid();
        paintpagin(e.NewPageIndex);

So I try to paint it

GridView2.PageIndex = newPageIndex;

        foreach (GridViewRow Rowe in GridView2.Rows)
        {
                CheckBox Rc = (CheckBox)Rowe.FindControl("rdaprobar");
                RadioButton Ri = (RadioButton)Rowe.FindControl("rdpendiente");
                RadioButton Rd = (RadioButton)Rowe.FindControl("rdCandelar");

                if (Rc.Checked == true)
                {
                    GridView2.Rows[Rowe.DataItemIndex].BackColor = ColorTranslator.FromHtml("#bcf5be");
                }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2025-02-20 02:43:30

甚至用于高光,格式化等?

使用网格行数据绑定事件。

因此,首先,我们的标记:

        <asp:GridView ID="GHotels" runat="server" CssClass="table" AutoGenerateColumns="false"
            width="45%" DataKeyNames="ID" OnRowDataBound="GHotels_RowDataBound" AllowPaging="True"
            OnPageIndexChanging="GHotels_PageIndexChanging"   >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
                <asp:BoundField DataField="LastName" HeaderText="LastName"    />
                <asp:TemplateField HeaderText="HotelName">
                    <ItemTemplate>
                        <asp:Label ID="txtHotel" runat="server" Text='<%# Eval("HotelName") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="270" />
            </Columns>
            <PagerStyle CssClass="GridPager" />
        </asp:GridView>

加载的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }

    void LoadData()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = 
                "SELECT * FROM tblHotels WHERE Description is not null ORDER BY HotelName";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                DataTable rstData = new DataTable();
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());
                GHotels.DataSource = rstData;
                GHotels.DataBind();
            }
        }
    }

我们的Pager Code(并注意您在Pager之前的网格负载 - CHEC您的代码!!!)。

    protected void GHotels_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GHotels.PageIndex = e.NewPageIndex;
        LoadData();

    }

好的,我们现在有了这个:

现在,如果每家酒店都活跃,我都会突出显示每家酒店。请注意,非常小心,我在GV中没有该列“活动”,但它是数据源的一部分。因此,对于奖励积分,我演示了如何获取/获取/使用您在GV中不呈现或具有的列,但实际上是数据源的一部分。

因此,我们要突出显示的代码:

    protected void GHotels_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // get binding data source                
            DataRowView gData = e.Row.DataItem as DataRowView;
            if ((bool)gData["Active"]) {
                // Label tHotel = e.Row.FindControl("txtHotel") as Label;
                // tHotel.BackColor = System.Drawing.Color.LightSteelBlue;
                e.Row.Cells[2].BackColor = System.Drawing.Color.LightSteelBlue;
            }
        }
    }

因此,我们现在拥有:

”在此处输入图像说明”

所以不要尝试每个GV的行 - 使用上面的行数据绑定以突出显示和格式。

The even to use for highlights, formatting, etc.?

use the Grid row data bound event.

So, first up, our markup:

        <asp:GridView ID="GHotels" runat="server" CssClass="table" AutoGenerateColumns="false"
            width="45%" DataKeyNames="ID" OnRowDataBound="GHotels_RowDataBound" AllowPaging="True"
            OnPageIndexChanging="GHotels_PageIndexChanging"   >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
                <asp:BoundField DataField="LastName" HeaderText="LastName"    />
                <asp:TemplateField HeaderText="HotelName">
                    <ItemTemplate>
                        <asp:Label ID="txtHotel" runat="server" Text='<%# Eval("HotelName") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="270" />
            </Columns>
            <PagerStyle CssClass="GridPager" />
        </asp:GridView>

Code to load:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }

    void LoadData()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = 
                "SELECT * FROM tblHotels WHERE Description is not null ORDER BY HotelName";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                DataTable rstData = new DataTable();
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());
                GHotels.DataSource = rstData;
                GHotels.DataBind();
            }
        }
    }

Our pager code (and NOTE YOU HAVE the load of the grid before the pager - chec your code!!!).

    protected void GHotels_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GHotels.PageIndex = e.NewPageIndex;
        LoadData();

    }

Ok, we now have this:

Now, I highlight each hotel if it is active. NOTE very careful, I don't have that column "active" in the GV, but it was/is part of the data source. So, for bonus points, I demonstrate how to get/grab/use columns that you don't render or have in the GV, but in fact is part of the data source.

So, our code to highlight, we have this:

    protected void GHotels_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // get binding data source                
            DataRowView gData = e.Row.DataItem as DataRowView;
            if ((bool)gData["Active"]) {
                // Label tHotel = e.Row.FindControl("txtHotel") as Label;
                // tHotel.BackColor = System.Drawing.Color.LightSteelBlue;
                e.Row.Cells[2].BackColor = System.Drawing.Color.LightSteelBlue;
            }
        }
    }

So, we now have this:

enter image description here

So do NOT try and loop each row of the GV - use the row data bound to highlight and format as per above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文