locked
Hightlighting a row in the gridview RRS feed

  • Question

  • User315403214 posted

    I have a current gridview that I get to highlight the selected row, but in my gridview I also have a buttonfield. This buttonfield will be a click for a modalpopup to print something. On the rowcommand I check the commandname and its always Select. Instead of being PrtJITBtn. Anyway around overwritting that particular cell?

    <asp:GridView ID="SMCPartListGV" runat="server" OnRowDataBound="SMCPartListGV_RowDataBound" AutoGenerateSelectButton="false" OnRowCommand="SMCPartListGV_RowCommand" HorizontalAlign="Center" EmptyDataText="No items have been entered" Width="100%"  OnSelectedIndexChanged="SMCPartListGV_SelectedIndexChanged" BorderColor="White" BorderStyle="Solid" PageSize="100" CellPadding="1" CellSpacing="1" RowStyle-BorderColor="White" CssClass="searsGV" GridLines="None">
                <AlternatingRowStyle BorderColor="White" />
                <RowStyle BackColor="White" />
                <Columns>
                    <asp:ButtonField ButtonType="Image" Text="JitBTN" CommandName="PrtJITBtn" Visible="true" ImageUrl="~/App_Themes/Theme1/Imgs/printBtn.png" />
                    <asp:TemplateField ShowHeader="False" HeaderText="Part Number">
                        <ItemTemplate>
                            <asp:HyperLink ID="hyperLnk" runat="server" NavigateUrl='<%# buildPartNbr(Eval("Item ID").ToString()) %>' Text='<%# Eval("Item ID") %>' ></asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    protected void SMCPartListGV_RowDataBound(object sender, GridViewRowEventArgs e)
            {
    
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(SMCPartListGV, "Select$" + e.Row.RowIndex);
                    e.Row.ToolTip = "Click to select this row.";
                }
    
                e.Row.Cells[2].Visible = false; 
    
            }
    
     protected void SMCPartListGV_RowCommand(Object sender, GridViewCommandEventArgs e)
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
    
                switch (e.CommandName)
                {
                    case "Something":
                        this.ModalPopupExtender2.Show();
                        break;
                    case "Select":
                        break;
                    case "Delete":
                        break;
                }
    
            }

    Wednesday, July 17, 2019 4:40 PM

All replies

  • User288213138 posted

    Hi griff303,

    According to your description, I couldn’t understand your requirement clearly.

    the commandname and its always Select. Instead of being PrtJITBtn

    CommandName="PrtJITBtn"

    You say commandname its always “select”, but in you ButtonField, the command name is “PrtJITBtn”.

    And I could not find another commandname in you code.

    What problem have you encountered? please post details information about your requirement and aspx,aspx.cs code.
     
    Best Regard,

    Sam

    Thursday, July 18, 2019 6:07 AM
  • User315403214 posted

    The problem is with the code below. It overwrites each cell with Select, so when you debug the commandname is always Select instead of PrtJITBtn. I'm not sure how to skip that or write back the commandname for the buttonfield.

     if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(SMCPartListGV, "Select$" + e.Row.RowIndex);
                    e.Row.ToolTip = "Click to select this row.";
                }

    Thursday, July 18, 2019 11:57 AM
  • User288213138 posted

    Hi griff303,

    According to your description, in row RowDataBound event,you set the CommandName=”select” in click property of the current row.

    But the CommandName in buttonfield  is PrtJITBtn, What are you doing this for?

     e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(SMCPartListGV, "Select$" + e.Row.RowIndex);

    And I'm not sure whether GetPostBackClientHyperlink must, but you can try to use other ways which did not set commandname to replace it.

    Best regards,

    Sam

    Tuesday, July 23, 2019 7:30 AM
  • User315403214 posted

    This is done so that the whole row can be selected. This is for a shopfloor that uses that to select the proper part with the sales order and kanban. I just wanted to incorporate a button so they can print their kanban and serial numbers from. But that select overrides the commandname in the button.

    Tuesday, July 23, 2019 12:49 PM
  • User288213138 posted

    Hi griff303,

     e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(SMCPartListGV, "Select$" + e.Row.RowIndex);

    This code is inside the RowDataBound () method, the RowDataBound () method is run again each time the page refreshes,so CommandName will always be Select each time it refreshes.

    Yu can try put GridView into UpdatePanel control. It Enables sections of a page to be partially rendered without a postback.

    Best regards,

    Sam

    Wednesday, July 24, 2019 9:46 AM