gridview 在编辑状态只能获取datakeys的值,获取其它列的值会出现(索引超出范围)
在网上搜了很久,不能解决。gridview在RowUpdating事件处理正常;但在rowupdating事件中获取其它列的值时出错。请高手帮忙看看设置了if (!IsPostBack)处理;设置了主键;主要后台代码(c#)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
protected void GridViewzfxx_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewzfxx.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridViewzfxx_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridViewzfxx.DataKeys[e.RowIndex].Value.ToString().Trim();
string zfje = GridViewzfxx.Rows[e.RowIndex].Cells[2].Text.ToString().Trim();//运行这条代码时出现索引超出范围的错误
} --------------------编程问答-------------------- 你第3列是模板列吗? --------------------编程问答-------------------- 没使用模板列。试过模板列GridViewzfxx.Rows[e.RowIndex].FindControl["htje"].Text.ToString().Trim();提示同样的错误。郁闷
前台gridview定义代码
<asp:GridView ID="GridViewzfxx" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" onpageindexchanging="GridViewzfxx_PageIndexChanging"
onrowcancelingedit="GridViewzfxx_RowCancelingEdit"
onrowdeleting="GridViewzfxx_RowDeleting" onrowediting="GridViewzfxx_RowEditing"
onrowupdating="GridViewzfxx_RowUpdating" Width="581px">
<Columns>
<asp:BoundField DataField="id" Visible="False" />
<asp:BoundField DataField="htbh_new" HeaderText="合同编号" ReadOnly="True" />
<asp:BoundField DataField="zfje" HeaderText="支付金额(元)" />
<asp:BoundField DataField="zfsj" DataFormatString="{0:yyyy-MM-dd}"
HeaderText="支付时间" />
<asp:CommandField HeaderText="编辑" ShowEditButton="True" CausesValidation="false"/>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
</Columns>
</asp:GridView> --------------------编程问答-------------------- 还有个问题在“编辑”commandfield不配置CausesValidation="false",不能触发 rowupdating事件,不知道为什么? --------------------编程问答-------------------- --------------------编程问答-------------------- 若是非模版列:
string zfje =((TextBox)GridViewzfxx.Rows[e.RowIndex].Cells[2].Controls[0]).Text; --------------------编程问答-------------------- 谢谢楼上的,我就是用这个语句,报index超出范围 --------------------编程问答-------------------- 执行这行代码也报index超出范围:GridViewRow row = GridViewzfxx.Rows[e.RowIndex];
但这行代码string id = GridViewzfxx.DataKeys[e.RowIndex].Value.ToString().Trim();
又能通过并获取的值正确。 --------------------编程问答-------------------- 这是完整代码:
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.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
//为GridViewzfxx绑定数据
private void GridViewBind()
{
string strhtbh = txbhtbh.Text;
//获取web.config中预设连接字符串
ConnectionStringSettings settings;
settings = System.Configuration.ConfigurationManager.ConnectionStrings["hxhtglConnection"];
String connectionString = settings.ConnectionString;
string strsql = "SELECT * FROM ht_zjzf WHERE htbh_new='" + strhtbh + "'";
try
{
SqlConnection conn = new SqlConnection(connectionString);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
GridViewzfxx.DataSource = ds;
GridViewzfxx.DataBind();
if (conn.State.ToString() == "Open") conn.Close();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void btnsearch_Click(object sender, EventArgs e)
{
GridViewBind();
}
//pageindex改变后的处理
protected void GridViewzfxx_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewzfxx.PageIndex = e.NewPageIndex;
GridViewBind();
}
//按取消键后的处理
protected void GridViewzfxx_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridViewzfxx.EditIndex = -1;
GridViewBind();
}
//按编辑键后的处理
protected void GridViewzfxx_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewzfxx.EditIndex = e.NewEditIndex;
GridViewBind();
}
//删除记录的处理
protected void GridViewzfxx_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridViewzfxx.DataKeys[e.RowIndex].Value);
//获取web.config中预设连接字符串
ConnectionStringSettings settings;
settings = System.Configuration.ConfigurationManager.ConnectionStrings["hxhtglConnection"];
String connectionString = settings.ConnectionString;
string strsql = "delete from ht_zjzf where id='" + id + "'";
try
{
SqlConnection conn = new SqlConnection(connectionString);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(strsql, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridViewzfxx.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
//更新记录的处理
protected void GridViewzfxx_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string strid = GridViewzfxx.DataKeys[e.RowIndex].Values[0].ToString();
string strzfje=((TextBox)GridViewzfxx.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string strzfsj=((TextBox)GridViewzfxx.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
ConnectionStringSettings settings;
settings = System.Configuration.ConfigurationManager.ConnectionStrings["hxhtglConnection"];
String connectionString = settings.ConnectionString;
string strsql = "update ht_zjzf set zfje='" + strzfje + "',zfsj='" + strzfsj + "' where id='" + strid + "'";
try
{
SqlConnection conn = new SqlConnection(connectionString);
if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(strsql,conn);
comm.ExecuteNonQuery();
Label2.Text = strzfje;
Label1.Text = strid;
Label3.Text = strzfsj;
comm.Dispose();
if (conn.State.ToString() == "Open")
conn.Close();
GridViewzfxx.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
}
--------------------编程问答-------------------- 顶,我也碰这样的问题,求解~~~ --------------------编程问答-------------------- 代码太长。。
这种问题一般都是ROWINDEX多或者CELL 了
Rows[e.RowIndex].Cells[2]
你看看这两个值 是否都满足你的GRIDVIEW
补充:.NET技术 , ASP.NET