简体   繁体   中英

How to mark records in gridview as read and unread

在此处输入图片说明

This is my GridView1. I want the latest record to be highlighted and after user click the authorization no(which user viewed the record in next page), the row will not be highlighted (means after the user view the record, the row is back to normal, no highlight no bold font).

My current progress is, I have created new bit field in my database named ReadStatus, and defaulted to 0 Next, I need to do the onrowdatabound coding in order to implement this.

first question is, do i need to read the bit column(ReadStatus) as I read all this column?AuthorizationNo, ProductID,Name,Qty,---(ReadStatus)?? should I read ReadStatus in this code?

           / /READING RECORD FROM TABLE TRACK_ITEM
            while (reader.Read())
            {
                MerchantProduct merchantProduct = new MerchantProduct();
                merchantProduct.TxID = reader["TxID"].ToString();
                merchantProduct.ProductID = reader["ProductID"].ToString();
                merchantProduct.Name = reader["ProductName"].ToString();
                merchantProduct.Qty = Convert.ToInt32(reader["Qty"]);

                listLatestProduct.Add(merchantProduct);
            }
            return listLatestProduct;

second is, can anyone show me the proper way to code in onrowdatabound?

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
           //Tried many code here but none is working 
    }

Thank you.

First you need to add one more column in ur db for ReadStatus(1/0), then make is as hiddenfield in your aspx page.

               <asp:TemplateField HeaderText="ReadStatus" Visible="false">
                                              <ItemTemplate>
                                                      <asp:Label ID="readStatus" runat="server"></asp:Label>
                                                      <asp:HiddenField ID="readStatusHiddenField" runat="server" Value='<%#Eval("ReadStatus") %>'/>
                                                  </ItemTemplate>
                                              </asp:TemplateField>

In your grid rowdataboun, just paste this code.It works for me

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // searching through the rows
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int reading = Convert.ToInt32(((HiddenField)e.Row.FindControl("readStatusHiddenField")).Value);
        if (reading == 0)
        {
            e.Row.BackColor = Color.LightGray;
            e.Row.Font.Bold = true;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM