如何使用链接按钮获取所选DataGridview行的值 [英] How to get values of selected row of DataGridview using link button

查看:54
本文介绍了如何使用链接按钮获取所选DataGridview行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我采用一个gridview,其中有两个链接按钮(用于编辑和删除)。

我使用了rowCommand属性进行更新和删除。删除它可以正常工作。

对于更新,我想要点击其编辑按钮的行的值。我尝试了很多方法。

但我没有得到它。

任何人都可以帮助我PLZ。



hereis我的代码.....



<前lang =cs> 受保护 void grdvwParts_RowCommand( object sender,GridViewCommandEventArgs e)
{
尝试
{
int id = Convert.ToInt32(e.CommandArgument);
if (e.CommandName.Equals( EditRecord))
{
// LinkBut​​ton lnkbtn = sender as LinkBut​​ton ;
// //获取特定行链接按钮
< span class =code-comment> // GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
// //获取特定行的用户ID
// < span class =code-comment> int userid = Convert.ToInt32(grdvwParts.DataKeys [gvrow.RowIndex] .Value.ToString());

// // int c oed = grdvwParts.Rows(id).cell(0).text;
// // GridViewRow dv =((LinkBut​​ton)sender).Parent.Parent as GridViewRow;
// string part = grdvwParts.DataKeys [id] .Value.ToString()。Trim();
// string code = Convert.ToString(DataBinder.Eval(Part code,prt_id));
// string section = Convert.ToString(DataBinder.Eval(Installation / Production,sdesc));
// tbcPrtCode.Text = code;
// < span class =code-comment> tbxPrtNm.Text = part;
// if(e.Row.RowType == DataControlRowType.DataRow)
// < span class =code-comment> {

tbcPrtCode.Text = e.Row.Cells [ 0 ]。文字;
// }
}
if (e.CommandName.Equals( DeleteRecord))
{
ScriptManager.RegisterStartupScript( this this .GetType(), alertmessage javascript:alert('Are') true );
con = new SqlConnection(strConnString);
cmd = new SqlCommand( String .Format( DELETE FROM Part WHERE(prt_id = + id + ),con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGridView();

}
}
catch (例外情况)
{
throw ex;
}
最后
{
如果(con != null
{
con.Close();
con.Dispose();
}
}

}







****************

和我的aspx文件是

 <   div     class   = 表格响应 > ;  
< < asp:GridView ID = grdvwParts runat = server AllowPaging = True

< span class =code-attribute> AllowSorting = True CssClass = table table-bordered AutoGenerateColumns = False

< span class =code-attribute> CellPadding = 0 GridLines = 宽度 < span class =code-keyword> = 100% ShowFooter = True

< span class =code-attribute> < span class =code-attribute> onrowcommand = grdvwParts_RowCommand

< span class =code-attribute> onselectedindexchanged = grdvwParts_SelectedIndexChanged >
< >
< asp:TemplateField HeaderText = Sr.No。 SortExpression = Sr.No。 ItemStyle-ForeColor = black >
< ItemTemplate >
< asp:标签 ID = SrNo runat = server < span class =code-attribute> 文本 =' <%#((GridViewRow)容器).RowIndex + 1 %> ' > < / asp:Label >
< / ItemTemplate >
< / asp:TemplateField >
< asp:BoundField DataField = prt_id HeaderText = 零件代码 / >
< asp:Bound字段 DataField = prt_nm HeaderText = 零件 / >
< asp:BoundField DataField = sdesc HeaderText = 安装/生产 / >
< asp:TemplateField HeaderText = >
< ItemTemplate >
< asp:LinkBut​​ton runat = server ID < span class =code-keyword> = lbtnEdit 文字 = 编辑 CommandArgument =' <%# Eval( prt_id%> ' CommandName = EditRecord > < < span class =code-leadattribute> / asp:LinkBut​​ton >
<% -
< a target = ' _ blank' href = ' Test.aspx?code = {0}' >
< asp:标签ID = lblEdit runat = server Text = 编辑 CommandArgument = ' <%#Eval(ID)%>' CommandName = EditRecord > < / asp:标签 >
< / a > - %>
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField HeaderText = >
< ItemTemplate >
< asp:LinkBut​​ton runat = server ID = lbtnRemove 文字 = 删除 CommandArgument =' <% #Eval( prt_id%> ' CommandName = DeleteRecord > < / asp:LinkBut​​ton >
<% - < a target = ' _ blank' href = ' Test.aspx?code = {0}' >
< asp:Label ID = lblremove runat = server Text = remove CommandArgument = ' <%#Eval(ID)%>' CommandName = DeleteRecord > < / < span class =code-leadattribute> asp:标签
>
< / a > - %GT;
< / ItemTemplate >

< / asp:TemplateField >
< /列 >
< / asp:GridView >
< / div >

解决方案

请尝试以下代码:

  protected   void  grdvwParts_RowCommand(对象 sender,GridViewCommandEventArgs e)
{
try
{
int id = Convert.ToInt32(e.CommandArgument);
if (e.CommandName.Equals( EditRecord))
{
string CustomerID =((Label)GridView1.Rows [e.RowIndex]
.FindControl( lblCustomerID))。文本;
string 名称=((TextBox)GridView1.Rows [e.RowIndex]
.FindControl( txtContactName))。文字;
string Company =((TextBox)GridView1.Rows [e.RowIndex]
.FindControl( txtCompany))。文字;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = 更新客户设置ContactName = @ ContactName, +
CompanyName = @ CompanyName其中CustomerID = @ CustomerID; +
从客户中选择CustomerID,ContactName,CompanyName;
// 更新数据
}
}
}



这里是从lblCustomerID和其他人那里获取值 - 所以你需要根据页面控件ID进行更改。


尝试如下...

 GridViewRow row =(GridViewRow)(((LinkBut​​ton)e.CommandSource).NamingContainer); 

var something = row.Cells [ 0 ]。文字;


Hi,
I take one gridview in which there are two link buttons (for edit and delete).
I used a rowCommand property for update and delete. For deletion it works properly .
For update I want values of of row whose edit button is clicked. I tried many ways.
But I didnt get it.
Can anybody help me plz.

hereis my code.....

protected void grdvwParts_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          try
          {
              int id = Convert.ToInt32(e.CommandArgument);
              if (e.CommandName.Equals("EditRecord"))
              {
              //    LinkButton lnkbtn = sender as LinkButton;
              //    //getting particular row linkbutton
              //    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
              //    //getting userid of particular row
              //    int userid = Convert.ToInt32(grdvwParts.DataKeys[gvrow.RowIndex].Value.ToString());

              //    //int coed=grdvwParts.Rows(id).cell(0).text;
              //    //GridViewRow dv = ((LinkButton)sender).Parent.Parent as GridViewRow;
              //    string part = grdvwParts.DataKeys[id].Value.ToString().Trim();
              //    string code = Convert.ToString(DataBinder.Eval("Part code", "prt_id"));
              //    string section = Convert.ToString(DataBinder.Eval("Installation/ Production", "sdesc"));
              //    tbcPrtCode.Text = code;
              //    tbxPrtNm.Text = part;
                  //if (e.Row.RowType == DataControlRowType.DataRow)
                 // {
                      tbcPrtCode.Text = e.Row.Cells[0].Text;
                 // }
              }
              if (e.CommandName.Equals("DeleteRecord"))
              {
                  ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' Are')", true);
                  con = new SqlConnection(strConnString);
                  cmd = new SqlCommand(String.Format("DELETE FROM Part WHERE(prt_id =" + id + ")"), con);
                  con.Open();
                  cmd.ExecuteNonQuery();
                  con.Close();
                  BindGridView();

              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
          finally
          {
              if (con != null)
              {
                  con.Close();
                  con.Dispose();
              }
          }

      }




****************
and my aspx file is

<div class="table-responsive">
                       <<asp:GridView ID="grdvwParts" runat="server" AllowPaging="True"

                             AllowSorting="True" CssClass="table table-bordered" AutoGenerateColumns="False"

                             CellPadding="0"  GridLines="None"  Width="100%" ShowFooter="True"

                             onrowcommand="grdvwParts_RowCommand"

                             onselectedindexchanged="grdvwParts_SelectedIndexChanged" >
          <Columns>
          <asp:TemplateField HeaderText="Sr.No." SortExpression="Sr.No." ItemStyle-ForeColor="black">
                 <ItemTemplate>
                       <asp:Label ID="SrNo" runat="server" Text='<%# ((GridViewRow) Container).RowIndex+1 %>'></asp:Label>
                  </ItemTemplate>
          </asp:TemplateField>
                 <asp:BoundField DataField = "prt_id" HeaderText = "Part code" />
                 <asp:BoundField DataField = "prt_nm" HeaderText = "Parts" />
                 <asp:BoundField DataField = "sdesc" HeaderText = "Installation/ Production" />
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lbtnEdit" Text="Edit" CommandArgument='<%# Eval("prt_id")%>' CommandName="EditRecord"></asp:LinkButton>
                  <%--
                    <a target='_blank' href='Test.aspx?code={0}'>
                        <asp:Label ID="lblEdit" runat="server" Text = "Edit" CommandArgument='<%# Eval("ID")%>' CommandName="EditRecord"></asp:Label>
                        </a>--%>
                        </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lbtnRemove" Text="Remove" CommandArgument='<%# Eval("prt_id")%>' CommandName="DeleteRecord"></asp:LinkButton>
                  <%--  <a target='_blank' href='Test.aspx?code={0}'>
                        <asp:Label ID="lblremove" runat="server" Text = "remove" CommandArgument='<%# Eval("ID")%>' CommandName="DeleteRecord"></asp:Label>
                        </a>--%>
                        </ItemTemplate>

                </asp:TemplateField>
               </Columns>
         </asp:GridView>
                  </div>

解决方案

Try below code:

protected void grdvwParts_RowCommand(object sender, GridViewCommandEventArgs e)
{
	  try
	  {
		  int id = Convert.ToInt32(e.CommandArgument);
		  if (e.CommandName.Equals("EditRecord"))
		  {
			string CustomerID = ((Label)GridView1.Rows[e.RowIndex]
								.FindControl("lblCustomerID")).Text;
			string Name = ((TextBox)GridView1.Rows[e.RowIndex]
								.FindControl("txtContactName")).Text;
			string Company = ((TextBox)GridView1.Rows[e.RowIndex]
								.FindControl("txtCompany")).Text;
			SqlConnection con = new SqlConnection(strConnString);
			SqlCommand cmd = new SqlCommand();
			cmd.CommandType = CommandType.Text;
			cmd.CommandText = "update customers set ContactName=@ContactName," +
			 "CompanyName=@CompanyName where CustomerID=@CustomerID;" +
			 "select CustomerID,ContactName,CompanyName from customers";
			// Update your data
		}
	}
}


Here it is fetching value from lblCustomerID and others - so you need to change as per your page control ID.


Try like below...

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

var something = row.Cells[0].Text;


这篇关于如何使用链接按钮获取所选DataGridview行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆