如何解决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内,我有一个Calendar,其中包含有关SelectionChanged事件的以下代码:
gvTabelaTeste.Rows[/*here should be the row index*/].Cells[4].Text = Calendario.SelectedDate.ToShortDateString();
我想做的是将单击的单元格的文本更改为日历上的选定日期,但是问题是我无法获得被单击的单元格行的索引。
我该怎么做?我尝试了不同的方法,但是直到现在,没有人为我工作。
解决方法
如何获取rowIndex已在此处回答:WPF- How to get selected row index in datagrid?
看看这是否符合您的需求。
,我相信您应该在GridView中的第一行“选择”行。之后,这很容易。
HTML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CalendarApp._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="OnSelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
</asp:Content>
后面的代码
protected void Page_Load(object sender,EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"),new DataColumn("Name"),new DataColumn("Country") });
dt.Rows.Add(1,"John Hammond","United States");
dt.Rows.Add(2,"Mudassar Khan","India");
dt.Rows.Add(3,"Suzanne Mathews","France");
dt.Rows.Add(4,"Robert Schidner","Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1,"Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
}
}
protected void OnSelectedIndexChanged(object sender,EventArgs e)
{
selectedIndex = GridView1.SelectedIndex;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == GridView1.SelectedIndex)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
row.ToolTip = string.Empty;
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
}
}
}
protected void Calendar1_SelectionChanged(object sender,EventArgs e)
{
GridView1.Rows[GridView1.SelectedIndex].Cells[0].Text = Calendar1.SelectedDate.ToString("d");
}
为您借用并改编自https://www.aspsnippets.com/Articles/Selecting-GridView-Row-by-clicking-anywhere-on-the-Row.aspx