首页 > 解决方案 > C#通过单击单元格获取GridView的行索引

问题描述

我有这个 GridView:

<asp:GridView ID="gvTabelaTeste" runat="server" AutoGenerateColumns="false" CssClass="w3-table w3-striped w3-white w3-hoverable w3-centered" OnRowDataBound="gvTabelaTeste_RowDataBound">
  <Columns>
    <asp:BoundField DataField="Status" HeaderText="Status"/>
    <asp:BoundField DataField="Atividade" HeaderText="Atividade"/>
    <asp:BoundField DataField="Analista" HeaderText="Analista"/>
    <asp:BoundField DataField="DtInicial" HeaderText="Data de abertura"/>
    <asp:BoundField DataField="DtFinal" HeaderText="Data de validade"/>
  </Columns>
</asp:GridView>

这两个div:

<div id="light" class="white_content">
  <h6 style="background-color: lightgray; margin-bottom: 0; margin-top: 0; text-align: center;"><strong>Escolher nova data de validade</strong></h6>
  <asp:Calendar ID="Calendario" runat="server" OnSelectionChanged="Calendario_SelectionChanged"></asp:Calendar>
</div>
<div id="fade" class="black_overlay"></div>

我将此代码放在 GridView 中的 RowDataBound 事件上:

e.Row.Cells[4].Attributes["onclick"] = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block';";

因此,当我单击每行的单元格 4 时,它会更改 div 的样式显示,使其类似于弹出窗口。在 div 我有一个日历,其中包含有关 SelectionChanged 事件的代码:

gvTabelaTeste.Rows[/*here should be the row index*/].Cells[4].Text = Calendario.SelectedDate.ToShortDateString();

我要做的是将单击的单元格的文本更改为日历上的选定日期,但问题是我无法获取单击的单元格行的索引。

我怎样才能做到这一点?我尝试了不同的方法,但直到现在没有人为我工作。

标签: c#htmlasp.net

解决方案


如何获取 rowIndex 已在此处得到解答:WPF- How to get selected row index in datagrid?

看看这是否符合您的需求。


推荐阅读