J.C
J.C

Reputation: 752

Select row should not include the first column datagridview asp.net

I would like the select rowdatabound not to included the first colunm. The reason for it is when i click on expand the datagridview the page refreshes causing the datagridview row to collapse again.
Here is a image of my table I have circuled in red the column I would like to remove from the rowdatabound. My current Datagridview

Here is my code for OnRowDataBound

    protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvInventario, "Select$" + e.Row.RowIndex);
            e.Row.ToolTip = "Haga clic para seleccionar esta fila.";
        }
    }

And here is my aspx code:

   <asp:GridView ID="gvInventario" runat="server" AutoGenerateColumns="false"  AllowSorting="true" ShowFooter="false" DataKeyNames="componente_id, ubicacion_id"
                ShowHeaderWhenEmpty="true" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging" OnRowDataBound = "OnRowDataBound" OnSelectedIndexChanged = "OnSelectedIndexChanged" 
                 CellPadding="3"  AllowColumResize="True" onsorting="grdDetails_Sorting" GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">    
                <Columns>
                      <asp:TemplateField ItemStyle-Width="20px">
                    <ItemTemplate >
                        <a href="JavaScript:divexpandcollapse('div<%# Eval("componente_id") %>');" >
                            <img id="imgdiv<%# Eval("componente_id") %>" width="9px" border="0" src="../images/plus.gif"
                                alt="" /></a>                       
                    </ItemTemplate>
                    <ItemStyle Width="20px" VerticalAlign="Middle"></ItemStyle>
                </asp:TemplateField>

                    <asp:TemplateField HeaderText="Min" SortExpression="cantidad_mini">
                        <ItemTemplate>
                         <asp:Label Text='<%# Eval("cantidad_mini") %>' runat="server" /> 
                        </ItemTemplate>
                        <EditItemTemplate>
                           <asp:TextBox ID="txtQuantityMin" Text='<%# Eval("cantidad_mini") %>' runat="server" />
                        </EditItemTemplate>
                    </asp:TemplateField>

                </Columns>
            </asp:GridView>

I would like the first column not to be included in rowdatabound.

Upvotes: 0

Views: 163

Answers (1)

VDWWD
VDWWD

Reputation: 35564

Instead of adding the onclick to a row you could add it to each cell individually and skip the first cell.

if (e.Row.RowType == DataControlRowType.DataRow)
{
    for (int i = 1; i < e.Row.Cells.Count; i++)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvInventario, "Select$" + e.Row.RowIndex);
        e.Row.ToolTip = "Haga clic para seleccionar esta fila.";
    }
}

Or add a class name tot the first cell in the RowDataBound or ItemStyle-CssClass in the aspx.

e.Row.Cells[0].Attributes["class"] = "noClick";

Then use jquery to prevent the clicking.

$('.noClick').click(function (e) {
    e.stopPropagation();
});

Upvotes: 1

Related Questions