【问题标题】:Select row should not include the first column datagridview asp.net选择行不应包含第一列 datagridview asp.net
【发布时间】:2019-08-06 01:04:25
【问题描述】:

我希望选择行数据绑定不包括第一列。原因是当我单击展开 datagridview 时,页面刷新导致 datagridview 行再次折叠。
这是我的表格的图像,我用红色圈出了我想从行数据绑定中删除的列。

这是我的 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.";
        }
    }

这是我的 aspx 代码:

   <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>

我希望第一列不包含在 rowdatabound 中。

【问题讨论】:

  • 请同时包含您的.aspx 代码。谢谢
  • 我添加了aspx代码

标签: c# asp.net datagrid


【解决方案1】:

您可以将onclick 单独添加到每个单元格并跳过第一个单元格,而不是将onclick 添加到一行。

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.";
    }
}

或者在 aspx 中 RowDataBoundItemStyle-CssClass 的第一个单元格中添加一个类名。

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

然后使用jquery来阻止点击。

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

【讨论】:

  • 是的,您的第一个解决方案有效,因为我不再使用调试器进行 oonrowdatabound。但是知道我也意识到 slectedIndexChange 是一个问题,因为它只关心所选的行。由于不允许更改我的问题,我将在这里提出一个新问题stackoverflow.com/questions/55182078/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2023-03-06
相关资源
最近更新 更多