我无法弄清楚我的代码有什么问题。我看了几个类似的帖子,没有运气。 我的代码从gridview返回第一行。我希望它返回选定的行。任何人都可以看到问题吗?
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table cellpadding="0" cellspacing="0">
<tr>
<td class="grid-white-body-left"></td>
<td class="grid-white-body-center">
<asp:GridView ID="gvTradeCallOffList" runat="server" AutoGenerateColumns="False" EmptyDataText="No Trade Call-Off(s) found."
GridLines="Horizontal" SkinID="SimpleBlackWhite" OnRowCommand="gvTradeCallOffList__RowCommand">
<Columns>
<asp:BoundField DataField="BookingID" HeaderText="Booking ID" SortExpression="BookingID"/>
<asp:BoundField DataField="RegistrationID" HeaderText="Registration ID" SortExpression="RegistrationID" />
<asp:BoundField DataField="BookingDateTime" HeaderText="Current Booking DateTime" SortExpression="BookingDateTime" />
<asp:BoundField DataField="CreatedDate" HeaderText="CreateD DateTime" SortExpression="CreatedDate" />
<asp:BoundField DataField="TradeName" HeaderText="Trade Name" SortExpression="TradeName" />
<asp:BoundField DataField="PreferredFirstName" HeaderText="Preferred Contact First Name" SortExpression="PreferredFirstName" />
<asp:BoundField DataField="PreferredLastName" HeaderText="Preferred Contact Last Name" SortExpression="PreferredLastName" />
<asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" />
<asp:TemplateField HeaderText="Contacted?">
<ItemTemplate>
<asp:TextBox ID="txtNotes" Text='<%# Bind("Notes") %>' TextMode="MultiLine" Height="70px" Width="300px" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSaveNotes" Text="Save Notes" CommandName="SaveNotes" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblSaveNotesSuccess" runat="server" Text="" EnableViewState="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
<td class="grid-white-body-right"></td>
</tr>
<tr>
<td class="grid-white-footer-left-roundL"></td>
<td class="grid-white-footer-center"></td>
<td class="grid-white-footer-right-roundL"></td>
</tr>
</table>
</asp:Content>
代码背后:
protected void gvTradeCallOffList__RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "SaveNotes")
{
int index;
bool bIsConverted = int.TryParse(e.CommandArgument.ToString().Trim(), out index);
GridViewRow selectedRow = gvTradeCallOffList.Rows[index];
TextBox txtNotes = (TextBox)selectedRow.FindControl("txtNotes");
string tradeCallOffListNotes = txtNotes.Text.Trim();
int bookingID = Convert.ToInt32(selectedRow.Cells[0].Text);
DataTable dt = new TradeBookingDAL().SaveTradeCallOffListNotes(bookingID, tradeCallOffListNotes);
if (dt.Rows.Count > 0)
{
string val = dt.Rows[0]["Notes"].ToString();
txtNotes.Text = val;
Label lblSaveNotesSuccess = (Label)selectedRow.FindControl("lblSaveNotesSuccess");
lblSaveNotesSuccess.Text = "Notes saved successfully";
lblSaveNotesSuccess.ForeColor = System.Drawing.Color.Green;
}
else
{
Label lblSaveNotesSuccess = (Label)selectedRow.FindControl("lblSaveNotesSuccess");
lblSaveNotesSuccess.Text = "Notes not saved successfully";
lblSaveNotesSuccess.ForeColor = System.Drawing.Color.Red;
}
}
}
catch (Exception ex)
{
new Util().LogError(ex);
}
}
答案 0 :(得分:1)
dt.Rows[0]
将始终按预期显示第一行,因为您在DataTable
中请求了第一项(0索引)。
您可以通过gridview的SelectedRow
属性获取所选行。然后,您可以使用此行的DataItemIndex
属性来了解它在基础数据表中的行。但由于您使用的是rowcommand
事件,这并不意味着该命令行也是所选行。
您可以将行绑定到CommandArgument
并分配索引或主键值,并在RowCommand
方法中使用此值,例如e.CommandArgument
。
答案 1 :(得分:0)
我认为应该是
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvTradeCallOffList.Rows[index];
在我的Rowcommand
中为我工作答案 2 :(得分:0)
感谢大家的帮助,但我已经找到了答案。
取代这些内容:
GridViewRow selectedRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
有了这个:
link