如何获取GridView中某行某列的值

2024-11-11 11:41:41
推荐回答(4个)
回答1:


<巧消head runat="server">













Font-Size="15pt" Text="新闻类型维护">
  新闻类型:    
Width="67px" />

                                           
CellPadding="4" ForeColor="#333333" GridLines="None" Width="566px"
onrowdeleting="GridView1_RowDeleting"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
>








oncheckedchanged="ckDleteAll_CheckedChanged"如芦 />
 删除全部





'>




'>





HorizontalAlign="Center" />














using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using User6Entity;
using User6Operate;

public partial class newstype : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bind();

}
}

private void bind()
{

NewsType Wl=new NewsType();
GridView1.DataSource = Wl.DataSetSql("select newsTypeID,newsTypeName from [newsType]");
GridView1.DataBind();

}

protected void Button1_Click(object sender, EventArgs e)
{
NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
string id;
id = Wl.ExecuteValue("select count(*) from [newsType] ");
id = txtName.Text +"_"+ DateTime.Now.Second.ToString() +"_" + id;
en.NewsTypeID = id;
en.NewsTypeName = txtName.Text;
if (!Wl.AddNewsType(en))
Response.Write(Wl.ShowMessage("添加失败"));
bind();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
Label txtid = (Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtID");
string id = txtid.Text;
en.NewsTypeID = id;
if(!Wl.DeleteNewsType(en))
Response.Write(Wl.ShowMessage("删除失败"));
bind();

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
string id;
//id =Wl .ExecuteValue ("select count(*) from newsType");
Label txtid = (Label)GridView1.Rows[e.RowIndex].Cells[1].FindControl("txtID");
TextBox txtname=(TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName");
//en.NewsTypeID = txtid.Text+"_"+DateTime .Now .Second +"_"+id ;
en.NewsTypeID = txtid.Text;
en.NewsTypeName = txtname.Text;
if (!Wl.UpdateNewsType(en))

Response.Write(Wl.ShowMessage("更新失败"));
GridView1.EditIndex = -1;

bind();

}
protected void ckDleteAll_CheckedChanged(object sender, EventArgs e)
{

CheckBox cbdeleteall = (CheckBox)sender;
if (cbdeleteall.Checked)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbdelete = (CheckBox)GridView1.Rows[i].FindControl("ckDelete");
if (cbdelete != null && !cbdelete.Checked)
{
cbdelete.Checked = true;
}
}
}
else
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cbdelete = (CheckBox)GridView1.Rows[i].FindControl("ckDelete");
if (cbdelete != null && cbdelete.Checked)
{
cbdelete.Checked = false;
}
}
}
}

protected void Button2_Click(object sender, EventArgs e)
{
NewsType Wl = new NewsType();
NewstypeEntity en = new NewstypeEntity();
for(int i=0;i {
CheckBox ckdelete = (CheckBox)GridView1.Rows[i].FindControl("ckDelete");
if(ckdelete!=null&&ckdelete.Checked)
{
Label lbid=(Label)GridView1.Rows[i].Cells[1].FindControl("txtID");
string id = lbid.Text;
en.NewsTypeID=id;
if(!Wl.DeleteNewsType(en))
Response.Write(Wl.ShowMessage("删除失败"));
}
}
bind();

}

}
大概代码 但是 我用的三层架构 不知道 你看得懂不

回答2:

string price = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblprice")).Text;

lblprice是你前台页面绑定“单价”Label控件的id


<裂罩ItemTemplate>
'>



*******************************************************************
你的时间激活错了,你激活了RowDeleted删除后的事件,应该是RowDeleting事件
GridView的属性里有个DataKeyNames属性,将它绑定表里的主键列空租
然后激活
RowDeleting事件

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id= GridView1.DataKeys[e.RowIndex].Value.ToString();
Response.Write ( id);
//把获取的id作为参数传给删除的方法

}

回答3:

GridView.Rows[i].Cells[j].Text

获得i行j列的值

回答4:

protected void GridView1_RowDeleting ( object sender, GridViewDeleteEventArgs e )
{
GridView grdProducts = sender as GridView;
string s = grdProducts.Rows [ e.RowIndex ].Cells [ 1 ].Text;

Response.Write ( s );
}