我有一个 girdivew 和一个 SQLDataSource 来将 dataTable 信息绑定到 gridview。
如何获取 Row UPDATING EVENT 中当前编辑行的某一列的值。(来自数据表)。
谢谢 ,
在下面的示例中,我在网格中显示数据,然后允许用户异步激活或停用评论。我正在使用 UPDATE PANEL 来异步激活它。
您需要同时使用 rowDataBound 和 RowCommand 事件来实现这一点
通过这种方式,您可以获得行的 id 并查看您想对该行执行的操作,无论是编辑、删除还是像我在此示例中所做的那样,我更新了一个 COLUMN
<asp:GridView ID="gvSHowMostViewedArticles" runat="server" AllowPaging="True"
AutoGenerateColumns="False" Width="920px" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
Font-Names="Verdana" Font-Size="X-Small" ForeColor="Black"
GridLines="Horizontal" PageSize="10" onrowdatabound="gvSHowMostViewedArticles_RowDataBound"
onrowcommand="gvSHowMostViewedArticles_RowCommand" onpageindexchanging="gvSHowMostViewedArticles_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="Sno">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ArticleTitle" HeaderText="Article Title" />
<asp:BoundField DataField="FullName" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField HeaderText="Message">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnShowMessage" runat="server" Text="Read" CommandName="showMessage" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activate">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnActivateComment" runat="server" Text="Activate" CommandName="ActivateComment" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="De Activate">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDeActivateComment" runat="server" Text="De-Activate" CommandName="DeActivateComment" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
protected void gvSHowMostViewedArticles_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Show Message
LinkButton lb = e.Row.FindControl("lnkBtnShowMessage") as LinkButton;
if (lb != null)
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb);
//Activate
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbActivate = e.Row.FindControl("lnkBtnActivateComment") as LinkButton;
if (lbActivate != null)
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbActivate);
lbActivate.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to Activate this comment " +
DataBinder.Eval(e.Row.DataItem, "ID") + "')");
}
//De Activate
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbActivate = e.Row.FindControl("lnkBtnDeActivateComment") as LinkButton;
if (lbActivate != null)
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbActivate);
lbActivate.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to De-Activate this comment " +
DataBinder.Eval(e.Row.DataItem, "ID") + "')");
}
}
protected void gvSHowMostViewedArticles_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Show Message
if (e.CommandName == "showMessage")
{
int sno = Convert.ToInt32(e.CommandArgument);
string strSql = "SELECT * FROM Comments WHERE comID = " + sno;
DataSet ds = DataProvider.Connect_Select(strSql);
lblCommentMessage.Text = ds.Tables[0].Rows[0]["comMessage"].ToString();
}
// Activate Comment
if (e.CommandName == "ActivateComment")
{
int sno = Convert.ToInt32(e.CommandArgument);
String strSql = "UPDATE Comments SET Visible = 1 WHERE ID = " + sno;
DataProvider.Connect_Select(strSql);
lblCommentMessage.Text = "Activated";
}
// De Activate Comment
if (e.CommandName == "DeActivateComment")
{
int sno = Convert.ToInt32(e.CommandArgument);
String strSql = "UPDATE Comments SET Visible = 0 WHERE ID = " + sno;
DataProvider.Connect_Select(strSql);
lblCommentMessage.Text = "Deactivate";
}
}
你试过什么?您是否仅尝试从某个列中获取数据?还是代表整行的某个 ID?如果是后者,您可以执行以下操作:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int ID = Convert.ToInt16(GridView1.SelectedDataKey.Value);
}
你必须在你的gridview中声明你的数据键名,像这样:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1"
onselectedindexchanged="GridView1_SelectedIndexChanged">
用户评论后编辑:
如果您想要某个列中的某个值,您可以这样做:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="handler"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
DataKeyNames="id" OnRowCommand="RowCommandHandler">
将 onRowCommand 添加到您的 gridview,然后在您的代码隐藏中:
protected void RowCommandHandler(Object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
int k = row.RowIndex;
string text = row.Cells[1].Text;
}
您获取所选行的索引,然后获取该索引并将其传递给 GridView1.Rows,然后您拥有选定的行,然后您可以从您想要的任何列获取数据。
string text = row.Cells[1].Text;
如果您需要文本的列是第 4 列,您只需将 Cells[1] 更改为 Cells[4] 等。