C# 单击gridview按钮后从列和行读取数据
我有一个gridview,每个生成的行都有gridview按钮。当我点击按钮时,它会向用户发送一封电子邮件,但我需要能够读取其中一列(WHERE列)及其行值,并将其包含在我的邮件正文中 这是我的标记代码C# 单击gridview按钮后从列和行读取数据,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个gridview,每个生成的行都有gridview按钮。当我点击按钮时,它会向用户发送一封电子邮件,但我需要能够读取其中一列(WHERE列)及其行值,并将其包含在我的邮件正文中 这是我的标记代码 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand" DataSourceID="prmtest" Wid
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand"
DataSourceID="prmtest" Width="994px" BorderStyle="None" GridLines="None"
onselectedindexchanged="GridView1_SelectedIndexChanged"
HorizontalAlign="Center">
<RowStyle ForeColor="#4D5763" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="Name_of_Training" HeaderText="Name of training"
SortExpression="Name_of_Training" />
<asp:BoundField DataField="Column1" HeaderText="Date" ReadOnly="True"
SortExpression="Column1" />
<asp:BoundField DataField="Start_time" HeaderText="Start time"
SortExpression="Start_time" />
<asp:BoundField DataField="End_time" HeaderText="End time"
SortExpression="End_time" />
<asp:BoundField DataField="Location" HeaderText="Where"
SortExpression="Location" />
<asp:BoundField DataField="Seats" HeaderText="Available seats" SortExpression="Seats" />
<asp:HyperLinkField HeaderText="Training documents"
NavigateUrl="url.zip" Text="Training material" Target="_self">
<ItemStyle ForeColor="#E5003B" Font-Underline="false"/>
</asp:HyperLinkField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Book"
OnClientClick="alert('The booking will be made for?')" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"
Text="Book" />
</ItemTemplate>
<ControlStyle Font-Bold="False" />
</asp:TemplateField>
</Columns>
<HeaderStyle ForeColor="#4D5763" BackColor="#DCDFE2" />
<EditRowStyle Font-Underline="False" />
</asp:GridView>
            
<asp:SqlDataSource ID="prmtest" runat="server"
ConnectionString="<%$ ConnectionStrings:dbprmtest %>"
SelectCommand="SELECT [Name of Training] AS Name_of_Training,
CONVERT(VARCHAR(10),[Date],20), [Start time] AS Start_time, [End time] AS End_time, [Location], [Seats] FROM [Sheet1$]">
</asp:SqlDataSource>
在“oncommand”事件处理程序中,首先找到包含to按钮的网格行的“所有者”
Button btn = (sender as Button);
GridViewRow grv = (GridViewRow)btn.NamingContainer;
GridRowView允许您访问基础数据源中的行(DataItemIndex),然后您可以从下面使用的任何/所有字段中提取该行
DataView dview = (DataView)prmtest.Select(DataSourceSelectArguments.Empty);
string str = (string)dview[grv.DataItemIndex]["Location"];
或者,如果它真的只是你需要的位置,那么也许就这么简单
Button btn = (sender as Button);
GridViewRow grv = (GridViewRow)btn.NamingContainer;
string str = gvAssets.Rows[grv.DataItemIndex].Cells[4].Text; // Cell[4] being the Location Col.
HTH…有两种选择: 第一种方法是发送命令参数中所需的数据。比如:
CommandArgument='<%#((GridViewRow)Container).RowIndex.ToString() +"|" + Eval("Anything").ToString() %>'
第二种方法是使用行索引转到行,并搜索您希望在邮件中包含的元素。我不确定我是否理解,现在必须创建另一个按钮,因为电子邮件代码被放置在row_命令中。
CommandArgument='<%#((GridViewRow)Container).RowIndex.ToString() +"|" + Eval("Anything").ToString() %>'
var arguements = CommandArguement.ToString().Split('|');