急。请教ASP C#中GridView出现的问题,谢谢各位大大,高分~求答案~谢谢~

我输入学号,出现学生数据,然后点击编辑,就可以更新此条学生数据,出错的那段代码如下:protectedvoidGridView1_RowUpdating(objectse... 我输入学号,出现学生数据,然后点击编辑,就可以更新此条学生数据,出错的那段代码如下:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 学生信息 set 学号='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',姓名'"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',性别='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "'出生日期='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',年级='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',专业='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',电话='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "',Email='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "',QQ='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString().Trim() + "', where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
}

当修改完毕,点击更新后,VS就把下列代码用黄色的框框起来

string sqlstr = "update 学生信息 set 学号='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',姓名'"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',性别='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "'出生日期='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',年级='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',专业='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',电话='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "',Email='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text.ToString().Trim() + "',QQ='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text.ToString().Trim() + "', where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

错误列表里面写的是:

无法将类型为“System.Web.UI.WebControls.Button”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”。

请问是什么原因啊?
说详细点好吗?我具体应该怎么修改?我0基础,所以。。。麻烦各位了

我把那段代码从Cells[2]依次修改到Cells[10],它又说

指定的参数已超出有效值的范围。
参数名: index

我到底应该怎么办啊

55555555555555555555555555

我问的不是问题的原因,而是怎么处理这个问题
展开
 我来答
83scharf
2008-10-17
知道答主
回答量:3
采纳率:0%
帮助的人:0
展开全部
下面是我写的一段代码,你对照着试试,因为我不知道你的是不是都是textbox的形式,正好这段代码里有自定义的列,你可以参考。
前台:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" CellPadding="4"
Font-Size="8pt" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" EmptyDataText="no note!" PageSize="5" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" Font-Names="Arial">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<HeaderStyle BackColor="MediumBlue" Font-Bold="True" ForeColor="#CCCCFF" Font-Size="Small" />
<Columns>
<asp:CommandField CancelText="cancel" DeleteText="delete" EditText="edit"
InsertText="insert" NewText="new" SelectText="select" ShowDeleteButton="True"
UpdateText="update" />
<asp:CommandField ButtonType="Button" CancelText="cancel" DeleteText="delete" EditText="edit"
InsertText="insert" NewText="new" SelectText="select" ShowEditButton="True" UpdateText="update" />
<asp:BoundField DataField="inquireId" HeaderText="inquire id" ReadOnly="True" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="fixedAssetId" HeaderText="fixed asset id" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="type">
<EditItemTemplate>
<%-- %><asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("type") %>'></asp:TextBox>--%>
<asp:DropDownList ID="ddl1" runat="server" DataSource='<%# ddl1bind()%>' DataValueField="type" DataTextField="type"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("type") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="soft type">
<EditItemTemplate>
<%-- %><asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("softType") %>'></asp:TextBox>--%>
<asp:DropDownList ID="ddl2" runat="server" DataSource='<%# ddl2bind()%>' DataValueField="softType" DataTextField="softType"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("softType") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="brandOrSoftname" HeaderText="brand or software name" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="productModelOrEdition" HeaderText="product model or edition" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="configuration" HeaderText="configuration" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="servicetag" HeaderText="servicetag" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="os" HeaderText="OEM" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="warranty" HeaderText="warranty" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="annualCost" HeaderText="annual cost" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="purchaseDate" HeaderText="purchase date" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="price" HeaderText="price" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="ownCompany" HeaderText="own company" ReadOnly="True" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="userNumber" HeaderText="user number" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="remark" HeaderText="remark" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
<EmptyDataRowStyle ForeColor="Red" />
<EditRowStyle Font-Size="Small" Wrap="False" />
</asp:GridView>

后台:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i;
//执行循环,保证每条数据都可以更新
for (i = -1; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ccffff'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('Are you want to delete\"" + e.Row.Cells[2].Text + "\"?')");
}
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from tb_ITInfo where inquireId='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["sql_XellaConnectionString"].ToString();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.ExecuteNonQuery();
conn.Close();
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string inquireid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string fixedassetid = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
string type = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl("ddl1")).SelectedItem.Text;
string softtype = "";
if (type == "软件")
{
softtype = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[5].FindControl("ddl2")).SelectedItem.Text;
}
else
{
softtype = "";
}
string brandorsoftname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text;
string model = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text;
string configuration = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).Text;
string servicetag = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).Text;
string os = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).Text;
string warranty = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[11].Controls[0])).Text;
string annualcost = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[12].Controls[0])).Text;
string purchasedate = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[13].Controls[0])).Text;
string price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[14].Controls[0])).Text;
string owncompany = (GridView1.Rows[e.RowIndex].Cells[15]).Text;
string usernumber = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[16].Controls[0])).Text;
string remark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[17].Controls[0])).Text;
DateTime date1 = Convert.ToDateTime(warranty);
DateTime date2 = Convert.ToDateTime(purchasedate);
int number = Convert.ToInt32(usernumber);
string sqlstr = "update tb_ITInfo set fixedAssetId='" + fixedassetid + "',type='" + type + "',softType='" + softtype + "',brandOrSoftname='" + brandorsoftname + "',productModelOrEdition='" + model + "',configuration='" + configuration + "',servicetag='" + servicetag + "',os='" + os + "',warranty='" + warranty + "',annualCost='" + annualcost + "',purchaseDate='" + purchasedate + "',price='" + price + "',ownCompany='" + owncompany + "',userNumber='" + number + "',remark='" + remark + "' where inquireId= '" + inquireid + "'";
if (brandorsoftname == "")
{
Response.Write("<script>alert('Brand or software name can't be empty!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0]));
}
else if (model == "")
{
Response.Write("<script>alert('Product model or edition can't be empty!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0]));
}
else if (warranty == "")
{
Response.Write("<script>alert('Warranty can't be empty!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[11].Controls[0]));
}
else if (warranty.Length < 8)
{
Response.Write("<script>alert('Please check the format of warranty!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[11].Controls[0]));
}
else if (purchasedate == "")
{
Response.Write("<script>alert('Purchase date can't be empty!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[13].Controls[0]));
}
else if (purchasedate.Length < 8)
{
Response.Write("<script>alert('Please check the format of purchase date!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[13].Controls[0]));
}
else if (price == "")
{
Response.Write("<script>alert('Price date can't be empty!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[14].Controls[0]));
}
else if (usernumber == "")
{
Response.Write("<script>alert('User number date can't be empty!')</script>");
this.Page.SetFocus((TextBox)(GridView1.Rows[e.RowIndex].Cells[16].Controls[0]));
}
else
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["sql_XellaConnectionString"].ToString();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
conn.Close();
GridViewBind();
}
}
catch
{
Response.Write("<script>alert('Please check the format!')</script>");
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
string P_Str_inquireid = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = ConfigurationManager.ConnectionStrings["sql_XellaConnectionString"].ToString();
SqlCommand myCmd = new SqlCommand("Proc_GetITInfo", myConn);
myCmd.CommandType = CommandType.StoredProcedure;
//添加参数
SqlParameter inquireId = new SqlParameter("@inquireid", SqlDbType.VarChar, 50);
inquireId.Value = P_Str_inquireid;
myCmd.Parameters.Add(inquireId);
//执行过程
myConn.Open();
SqlDataReader rd = myCmd.ExecuteReader();
if (rd.Read())
{
HiddenField1.Value = rd["type"].ToString();
HiddenField2.Value = rd["softType"].ToString();
}
rd.Close();
myCmd.Dispose();
myConn.Close();
((DropDownList)GridView1.Rows[e.NewEditIndex].Cells[4].FindControl("ddl1")).SelectedValue = HiddenField1.Value;
((DropDownList)GridView1.Rows[e.NewEditIndex].Cells[5].FindControl("ddl2")).SelectedValue = HiddenField2.Value;
}
public SqlDataReader ddl1bind()
{
string sqlstr = "select distinct type from tb_Type where typeId!=0";
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["sql_XellaConnectionString"].ToString();
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
return sqlcom.ExecuteReader();
}
public SqlDataReader ddl2bind()
{
string sqlstr = "select distinct softType from tb_SoftType";
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["sql_XellaConnectionString"].ToString();
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
return sqlcom.ExecuteReader();
}
微测检测5.10
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇... 点击进入详情页
本回答由微测检测5.10提供
好片每日推荐
2008-10-09 · TA获得超过666个赞
知道小有建树答主
回答量:439
采纳率:0%
帮助的人:364万
展开全部
你的更新语句 姓名字段少了个=号..

Cells[1]到Cells[9]分别代表第2列到第10列..这其中有一列中间是Button控件,你检查一下...
-------------------------
GridView1.Rows[e.RowIndex].Cells[1].Controls[0])
这一句的意思是GridView更新行的第2列的第1个控件。。
Cells是单元格的意思,参数从0开始,Cells[1]也就是第2个单元格即第2列..
你出现错误的原因可能是你的GridView里的Cells里有两个控件,比方说同时有TextBox和BUTTON,你可以用Controls[参数]来选择哪一个控件,参数从0开始,0就是1...
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
瑾记于心
2008-10-10
知道答主
回答量:6
采纳率:0%
帮助的人:0
展开全部
报类型不能转换的错误,应该是你的单元格里的控件有类型是button,button的当然不能在程序中用textbox的类型来强制转换啊。另外,有效范围超出,是因为如果你的gridview如果只有10列,第10个单元格不是CELL[10]而是cell[9],记住,一般都是从0开始为第一个的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
miniapp3wwC76odT2M2F
2008-10-08 · TA获得超过879个赞
知道小有建树答主
回答量:291
采纳率:0%
帮助的人:259万
展开全部
就是说你的类型是button,你偏要把它转换成 textbox当然就不行,

就像是一个字符串是aaa
string str="aaa";

你将它转换成数字,当然会出错了。
int m=(int)(str);

这样写肯定会出错的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
chenweidi232
2008-10-23 · TA获得超过674个赞
知道小有建树答主
回答量:858
采纳率:50%
帮助的人:402万
展开全部
你肯定哪个地方button=TextBox了,你发的那段代码根本就没有这样的错误。

还有你这样写不是很好,用FindControl("Control_id")as ControlClass).text这样更好一点.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(12)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式