C# gridview 子控件控件在输入数字时,偶尔会出现按一下键盘,结果会输入两个一模一样的数,是怎么回事啊?

DevExpress控件GridView&不显示数据
DevExpress控件GridView 不显示数据
DevExpress.XtraGrid.Views.Grid.GridView控件有时候会出现数据不显示的情况,但实际上数据是存在的。这需要将每个列的FieldName字段设置为相应的数据库字段名,在代码中添加“this.gridView1.PopulateColumns();”
数据就会显示出来,但是列名的显示就会成为数据库字段名,可以用如下方法修改
gridView1.Columns[0].Caption = "市场编号";
&&&&&&&&&&&
gridView1.Columns[1].Caption = "市场名称";
&&&&&&&&&&&
gridView1.Columns[2].Caption = "地区名称";
&&&&&&&&&&&
gridView1.Columns[3].Caption = "详细通讯地址";
&&&&&&&&&&&
gridView1.Columns[4].Caption = "联系人";
&&&&&&&&&&&
gridView1.Columns[5].Caption = "联系电话";
&&&&&&&&&&&
gridView1.Columns[6].Caption = "网址";
&&&&&&&&&&&
gridView1.Columns[7].Caption = "电子邮箱";
&&&&&&&&&&&
gridView1.Columns[8].Caption = "备注";
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。页面导航:
→ 正文内容 GridView控件操作
C#与SQL连接:GridView控件对数据库的操作
GridView控件操作方面的知识,需要的朋友可以参考一下
GridView和DataGrid的异同
GridView 是 DataGrid的后继控件,在.net framework 2 中,虽然还存在DataGrid,但是GridView已经走上了历史的前台,取代DataGrid的趋势已是势不可挡。GridView和DataGrid功能相似,都是在web页面中显示数据源中的数据,将数据源中的一行数据,也就是一条记录,显示为在web页面上输出表格中的一行。
GridView相对于DataGrid来说,具有如下优势,功能上更加丰富,因为提供了智能标记面板(也就是show smart tag)更加易用方便,常用的排序、分页、更新、删除等操作可以零代码实现!具有PagerTemplate属性,可以自定义用户导航页面,也就是说分页的控制更加随心所欲。GridView和DataGrid在事件模型上也多有不同之处,DataGrid控件引发的都是单个事件,而GridView控件会引发两个事件,一个在操作前发生,一个在操作后发生,操作前的事件多位***ing事件,操作后的事件多位***ed事件,比如Sorting 事件和sorted 事件,RowDeleting和RowDeleted事件。
GridView操作初步
1、显示数据源中的数据
从ToolBox中选取GridView控件拖到页面上,然后点击右键,选择Show Smart Tag,在Choose Data Source中选择 New Data Source, 出现Data Source Configuration Wizard,选择连接字符串,可以选择已经存放在web.config中的ConnectionString ,然后可以选择是使用存储过程,还是从表或视图中选择数据。
在这一步,左侧的Where语句可以指定查询条件,点击Where,出现Add Where Clause ,选择要设定条件的列,操作符是等于还是like还是其它,然后选择Source,也就是说要限定的条件从哪里取值,可以是Control、Session、Form、Cookie、QueryStirng等,如果选择Control,那么需要在右侧,选中是那个控件,然后还可以设定默认值,设定完后系统自动生成Sql 表达式和值的表达式,此时点击Add按钮,完成条件的添加,Where Cluase下出现刚刚添加的条件。如果没有点击Add,很容易设定了条件,但是因为没有添加到Where子句中,所以不起作用。
在这一步,左侧的Order By,可以让我们设定排序列,就是我们取出的记录要按照什么派逊,可以设定三个列,是升序还是降序。
在这一部,左侧的Advanced,可以设定Advanced Sql Generation Options,这里可以生成这个查询的Insert、update、Delete语句,当然,前提是您选择的字段中必须包含了主键。当您想在GridView中不编写任何代码实现对表格的编辑、删除等操作时,就必须在配置数据源时,在这里生成Insert、Update、Delete这些语句。 GridView中编辑删除等操作的零代码,就是根据配置数据源时自动生成的这些语句来完成数据源的更新删除等操作的。
在最后一步,您可以测试一下您刚刚生成的查询是否正确,最后点击完成,数据已经出现在页面上了,按Ctrl+F5运行。
恭喜您!您已经会使用Asp.net来显示数据库中的数据了。
2、让GridView可以分页
GridView把数据显示出来了,但是那么多条记录罗列到一页上是不合适的,我们应该对数据进行分页。还记得在asp时代,分页是多么的麻烦,需要编写很多的代码,而且各种分页组件也应运而生。而在GridView中,您会发现,分页是如此的简单,只需要您轻点鼠标,在Show Smart Tag中,选中Enable Paging,表格的分页操作变完成了,是不是So Easy呢。
并不是什么数据源都可以让GridView实现自动分页的,比如如果DataSourceMode是DataReader,就无法实现自动分页。而且只有ObjectDataSource是界面级别的支持分页,类似我们常用的SqlDataSource都是先提取所有的记录,然后只显示本页需要显示的记录,然后抛弃其余的记录,都有点浪费资源的啦!
当GridView的AllowPaging属性设置为True的时候,我们实现了分页,我们还可以对分页进行一些个性化的设置。常用的属性包括:PageIndexDD设置数据显示的当前页面,默认是0,也就是数据的首页。PageSize DD也就是一页显示多少条记录,默认为10条。在PagerSettings中,还可以对分页的导航按钮进行详细设置,在Mode属性中,可以设置:NumericDD默认的,分页用数字表示,1,2,3……。NextPrevious、NextPreviousFirstLast、NumericFirstLast均可顾名思义,显示上一页、下一页、首页、末页等。当Mode设定不是Numeric时,那么可以通过设定FirstPageText、LastPageText等属性来实现分页导航时,到首页、末页、下页、上页时显示的文字提示。
如果想实现分页界面的完全自动控制,还可以点击GridView右键,选择编辑模版-PagerTemplate来实现,在模版中加入若干个Button控件,然后将Button控件的CommandName属性设置为Page,将CommandArgument属性分别设置为First、Last、Prev、Next或者一个数字,即可实现分页操作。
3、GridView中的编辑、删除、排序
数据从数据源中提取出现,显示在网页上后,我们如果需要对其中的数据进行编辑、更新、删除等操作,还是不需要编写任何代码,利用GridView内置的功能即可实现。
在智能标记中,点击编辑列,在Avaliable Fields中,选择ComandField,然后双击Edit,update,cancel和Delete,我们就为GridView添加了编辑和删除功能。如果在配置数据源的时候,我们已经生成了Insert、update、delete这些语句,那么我们现在就可以执行程序。点击页面上的Edit,出现Update和Cancel按钮,同时当前行除了主键以外的列,数值都放在了一个文本框中,可以进行编辑,然后点击Update即可保存。点击Delete,删除当前行记录。您是不是已经被GridView强大的功能折服了呢?
在Show Smart Tag中,选择Enable Sorting,这时所有列的Header都变成了一个超链接,其实这些都是一个LinkButton控件,运行代码,在网页生成的数据表中,点击第一行中的列名,即可按照当前列进行排序,再次点击则反向排序。
如果您只需要对其中的几列进行排序,可以在智能标记中,选择编辑列,选中要排序的列,然后在右侧的属性中找到SortExpression属性,然后从下拉框中选择根据哪个字段排序,一般当然是当前字段咯,完成排序的设置。如果您不需要这一列参与排序,那么只需要把此列的SortExpression属性后面的值删除,也就是说设置成空字符串即可。试一试,是不是排序已经尽在掌握之中了呢?
GridView中的自定义列
GridView可以根据数据源自动生成列,但是如果我们需要自定义列的显示方式,让GridView的列完完全全的由我们自己来控制,我们就需要用到一种特殊的列DDTemplateField。因为GridView生成的列都是一个字段一列,如果我们需要把两个字段合并为一列显示呢?我们可以使用模板列。我们可以指定包含标记和控件的模版,自定义列的布局和行为,我们可以新建一个模版列,也可以直接把已经生成的列转换为模版列来进行个性化的设置。
在GridView上单击右键,选择编辑模版,在弹出菜单中选择要编辑的列,出现列模版的编辑画面。其中HeaderTemplateDD自定义列的标头部分显示的内容,FooterTemplateDD脚注部分显示哪谌荩?/SPAN&ItemTemplateDD是打开网页后此列数据显示的内容,EditItemTemplateDD此列处于编辑状态时如何显示,AlternatingItemTemplateDD交替项显示的内容,也就是说为了显示效果,可以隔行分别以不同的风格显示。
Example 1:
我们现在假设有一个表,其中有一个字段是username,我们现在产生一个自定义列,自定义列中包含此人的照片,同时我们假定照片的路径为image/username.jpg。我们首先右键点击GridView,在智能标记中,选择编辑列,添加一个模版列,然后编辑模版中的ItemTemplate,加入一个Image控件,然后右键点击Image控件,选择Edit DataBindings,在ImageUrl中设置Field Binding,首先我我要Bound to 数据源中的某列,因为所有图片的路径和格式是相同的,唯有名字不同而已,所以我们这里选中username字段,在format中,我们要自己定义其格式,输入image/{0}.jpg, {0}代表的就是上面绑定的字段,下面有一个Two Way DataBinding 的复选框,就是是否双向绑定的意思,如果单向绑定,一般采用Eval,也就是说数值只从数据源传到页面上,如果双向绑定,也就是采用Bind的话,对数据的修改可以回传到数据源之中。
在web页面执行时,不同的行因为username不同,图片的名字也会做出相应的替换。点击确定,然后执行当前网页,我们就可以看到在我们的自定义列中显示出了用户的照片。
Example 2:
在数据库中,存储性别的时候,一般采用bit数据类型,存储为True或者False,在GridView自动生成列的时候,一般使用CheckedBoxField 列来显示bit类型的数据,显示在网页上就是一个单选框,如果选中,也就是Checked了,就是男的,否则就是女的。这样看起来,很不直观,下面我们通过自定义列将性别显示为男、女。
首先在GridView的Show Smart Tag中,选择编辑列,然后双击TemplateFields ,添加了一个模版列,确定后,点击右键选择编辑模版,选中刚添加的列。在ItemTemplate中添加一个DropListDown控件,然后编辑它的数据绑定,Edit DataBinding,把SelectedValue属性绑定到性别列。
在DropListDown控件中选择 Edit Item,就是编辑其下拉列表的项,我们添加两个Item,一个的Text属性是男,Value设置为True,一个的Text属性设置为女,Value属性设置为False。到这里,你明白了么?因为DropDownList控件的显示文本和其值是可以不一样的,我们用数据绑定取到了性别这一列的值,True或者False,然后反映到DropDownList控件上,如果值为True,因为Text属性为男的Item的Value为True,所以我们现在运行网页,在新添加的列中,显示的不再是单选框或者True、false这些没有含义的东西,而是以下拉列表的方式显示出了当前用户是 男还是女。
自定义列中的数据更新
假设数据库中有一个"权限"字段,值为0代表未审核用户,为1代表一般用户,为9则代表管理员用户。根据前面所说的自定义列的办法,通过对DropListDown的绑定,在网页中显示权限为 "管理员",而不是显示数字9。问题产生了,如果我们调整用户权限的话,比如把一般用户更改为管理员,在编辑模版中的用户权限的下拉列表,如何把它的值返回给数据源来完成更新操作。
我们在EditItemTemplate中设置的DropListDown控件,必须选中了Two Way DataBinding,也就是数据双向帮定,这样才能返回数据。前面我们谈到,在GridView中,事件不是单个的,是两个,一个是发生前,一个是发生后,因为我们需要在数据更新前把下拉列表的权限值传送出去,所以我们需要对GridView1_RowUpdating 进行编码,编码如下:
代码如下:protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)& {&& //当前编辑的是哪行?&&&&&&&&&& int index = GridView1.EditI&&&& //取得当前编辑行的GridViewRow对象&&&&&&&&&& GridViewRow gvr = GridView1.Rows[index];&&&& //在当前行中,寻找DropListDown控件&&&&&&&&& DropDownList dp = (DropDownList)gvr.FindControl("editdrop");&&&& //将DropListDown的值赋给NewValues集合中的权限字段。&&&&&&&&& e.NewValues["rights"] = dp.SelectedV& } RowDataBound事件
在创建gridView控件时,必须先为GridView的每一行创建一个GridViewRow对象,创建每一行时,将引发一个RowCreated事件;当行创建完毕,每一行GridViewRow就要绑定数据源中的数据,当绑定完成后,将引发RowDataBound事件。如果说我们可以利用RowCreated事件来控制每一行绑定的控件,那么我们同样可以利用RowDataBound事件来控制每一行绑定的数据,也就是让数据如何呈现给大家。
还举同样的例子,在数据表中,存在性别列,上面我们用DropListDown控件的DataBounding来表示出了中文的性别,但是毕竟不太美观,我们现在可以利用Label控件和RowDataBound事件来实现完美的中文性别显示。RowDataBound,
首先,还是把性别列,设置为模板列,并添加一个Label控件,将Label控件绑定到数据源的性别段,然后我们在GridView控件属性的事件列表中双击RowDataBound,生成如下事件:
代码如下:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)&& {&& //判断当前行是否是数据行&&&&&&&&& if (e.Row.RowType == DataControlRowType.DataRow)&&&&&&&&&& {& //用FindControl方法找到模板中的Label控件&& Label lb1= (Label)e.Row.FindControl("Label1");& //因为RowDataBound是发生在数据绑定之后,所以我们可以& //判断Label绑定的数据,如果是True,就更改其text属性为男&&&&&&&&&&&&&&&&& if (lb1.Text== "True")&&&&&&&&&&&&&&&&&&&&&&&& lb1.Text = "男";&&&&&&&&&&&&&&&&& else&&&&&&&&&&&&&&&&&&& lb1.Text = "female";&&&&& }&&&&& }& RowType
RowType可以确定GridView中行的类型,RowType是玫举变量DataControlRowType中的一个值。RowType可以取值包括 DataRow、Footer、Header、EmptyDataRow、Pager、Separator。很多时候,我们需要判断当前是否是数据行,通过如下代码来进行判断 :
if (e.Row.RowType == DataControlRowType.DataRow) RowDeleting和RowDeleted事件
RowDeleting发生在删除数据之前,RowDeleted发生在删除数据之后。
使用RowDeleting事件,可以在真正删除前再次确认是否删除,可以通过设置GridViewDeleteEventArgs.Cancel=True来取消删除;也可以用于判断当前数据库记录数,如果只剩一条记录且数据库不能为空则提示并取消删除操作。
使用RowDeleted事件,可以在删除后,通过GridViewDeletedEventArgs的Exception属性判断删除过程中是否产生异常,如无异常,则可以显示类似于” 1 Records deleted” 之类的提示信息。
代码如下:&protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)&& &{&& &//取得当前行号,并取得当前行的GridViewRow对象&&&&&&&&& &int index=e.RowI&&&&&&&&& &GridViewRow gvr=GridView1.Rows[index];&& //取得当前行第二个单元格中的文字&& &str1 = gvr.Cells[1].T&& //进行提示&&&&&& &Message.Text& ="您将删除一个用户,其姓名为"+str1 ;&&&&& &}&&&&& &protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)& &{&& &//如果没有产生异常,则提示成功删除,否则提示删除失败&&&&&&&&&& &if (e.Exception == null)&&&&&&&&&&&&& &Message.Text += "& br&您成功删除了"+str1 ;&&&&& &else&&&&&&&& &Message.Text += "删除失败,请联系管理员"; &} RowEditing事件
在GridView中的行进入编辑模式之前,引发RowEditing事件,如果您需要在编辑记录前进行某些预处理,可以在这里操作。如果想取消对当前行的编辑,可以把GridViewEditEventArgs 对象的 Cancel 属性设置为 true即可。
代码如下:&protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) &{&& &//用NewEidIndex取得当前编辑的行号,然后获取gridviewrow对象&&&&&&&&& &GridViewRow gvr = GridView1.Rows[e.NewEditIndex];&&&& &//判断,如果当前编辑行姓名栏为admin用户,则取消对当前行的编辑&&&&&&&& &if (gvr.Cells[1].Text =="admin")&&&&&&& &e.Cancel =& &}&& RowUpdating和RowUpdated事件
RowUpdating事件发生在更新数据源之前,RowUpdated发生在更新数据源之后。
我们可以在记录更新前利用RowUpdating做一些预处理工作,比如修改密码时,因为密码在数据库中不是明文存储,进行了hash,所以在更新密码前,应该生成其hash值,再进行更新操作。RowUpdated则可以检验更新是否成功。
代码如下:protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {&&&&&&&& GridViewRow gvr = GridView1.Rows[GridView1 .EditIndex& ];&& //寻找输入密码的控件&&&& TextBox tb1 = (TextBox)gvr.FindControl("tb_password");&& //将此控件中的文本hash后,把password存入NewValues这个字典中&&&&&&&& e.NewValues ["password"] =tb1.Text .GetHashCode().ToString () ;&&&&&& }&&&&&& protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)& {& //如无异常,则更新成功&& if (e.Exception == null)& Message.Text += "更新成功!"; }&& Keys、OldValues、NewValues集合
Keys字典中一般存放的是数据源中的主键字段的key和value的对应值,如果主键由多个字段组成,那么Keys为每个键字段添加其字段名称和值。OldValues中存放的是要更新的行的字段名和原始值,每个字段为其中的一项。NewValues中存放的是要更新的行的字段名和修改后的值,每个字段为其中的一项。注意,主键字段只存放于keys集合中。
这三个集合中的每一项都是DictionaryEntry类型的对象,我们可以用DictionaryEntry.Key来确定一个项的字段名称,用DictionaryEntry.Value来确定某项的值。
在上面的例子中,为了把密码明文加密后再存入数据库,我们利用了NewValues字段,重新设置key为password的项的值。为了保证安全性,我们在更新数据前对NewValues中的所有值进行html编码:
代码如下:protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)& { //遍历NewValues,取得其中每一对DictionaryEntry对象&
foreach (DictionaryEntry de in e.NewValues)&&
//de.key就是字段名,如果此处单独更新某字段的话,也可以直接填写字段名,//比如 e.NewValues[“password”]&&&&&&
e.NewValues[de.Key] = Server.HtmlEncode(de.Value.ToString());&& }&& Example2:
代码如下:protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){&
//分别利用Keys、OldValues、NewValues取得主键名、原始数据和更新后数据&
Message .Text& = e.Keys["username"] + "的email地址从" + e.OldValues["email"] + "变更为" + e.NewValues["email"];&
}&& GridView控件操作方面的知识就介绍到这里。
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行
12345678910急。请教ASP C#中GridView出现的问题,谢谢各位大大,高分~求答案~谢谢~_百度知道
急。请教ASP C#中GridView出现的问题,谢谢各位大大,高分~求答案~谢谢~
&&#39,然后点击编辑.Rows[e:无法将类型为“System.ToString().Rows[e.RowIndex].Trim() + &&quot, where id=&#39我输入学号.Cells[6].RowIndex].Trim() + &&quot.Trim() + &.T
+ ((TextBox)(GridView1;
string sqlstr = &quot.Text.RowIndex].Close();
+ ((TextBox)(GridView1;&quot.Controls[0])).Text.Rows[e;&#39.Controls[0])).Controls[0])).ToString().UI.Cells[5];.Trim() + &quot.T
sqlcom = new SqlCommand(sqlstr.Value.ToString();
+ ((TextBox)(GridView1;&#39.RowIndex].Cells[1];出生日期='
+ ((TextBox)(GridView1;'.Cells[2].Text,QQ=&#39.RowIndex].Text.ToString();.Text.Text.Controls[0])).Text,Email='&quot.Trim() + &quot.Cells[7];
+ GridView1;
+ ((TextBox)(GridView1;&#39.Text.Web.Cells[7];
+ ((TextBox)(GridView1;错误列表里面写的是.Controls[0])),性别=&#39.ToString().Rows[e.RowIndex].Controls[0])).RowIndex],专业='&
+ ((TextBox)(GridView1,姓名&#39.Cells[4].Text.Controls[0]));.Controls[0])).Trim() + &quot.Text.Text.Trim() + &quot.ToString();&quot.ToString();&#39.ToString().Cells[8].TextBox”;
+ GridView1;;&quot.Trim() + &quot.Text.Trim() + &quot.ToString() + &quot.RowIndex].Rows[e.EditIndex = -1.Controls[0]));&#39.Trim() + &,电话='&#39.Rows[e.Cells[3];
sqlcon.WebControls.Controls[0])).RowIndex];
GridView1,Email=&#39.RowIndex],出错的那段代码如下;
+ ((TextBox)(GridView1.Rows[e.Rows[e.Cells[6];;&#39.DataKeys[e;&&quot.DataKeys[e.Trim() + &&quot.RowIndex].Cells[9];'
+ ((TextBox)(GridView1;'
bind().Rows[e.W&出生日期=&#39.Controls[0]));.Trim() + &quot.Trim() + &quot.Controls[0])).ToString();&quot.ExecuteNonQuery();&#39.Text.ToString(), where id=&#39,性别=&#39.ToString() + &quot.Rows[e;'&quot.Controls[0])).Trim() + &&#39.Controls[0]));'&#39,年级='&.T;
+ ((TextBox)(GridView1.Cells[9];.Trim() + &
}当修改完毕,年级=&#39.Controls[0])).WebControls.RowIndex];&&quot,专业=&#39。请问是什么原因啊;&
+ ((TextBox)(GridView1;;.Rows[e.Cells[3].Controls[0])):
protected void GridView1_RowUpdating(
+ ((TextBox)(GridView1,VS就把下列代码用黄色的框框起来string sqlstr = &quot.Cells[5],点击更新后.Cells[8];
+ ((TextBox)(GridView1.Rows[e.Trim() + &quot,出现学生数据.RowIndex];update 学生信息 set 学号=&#39.RowIndex].Trim() + &quot, sqlcon).Controls[0]));.RowIndex];&quot.Text.RowIndex].ToString().UI,QQ=&#39.Rows[e;&#39.Rows[e, GridViewUpdateEventArgs e)
sqlcon = new SqlConnection(strCon).RowIndex];&quot.Value,电话='
+ ((TextBox)(GridView1;
+ ((TextBox)(GridView1.RowIndex].Cells[4];
+ ((TextBox)(GridView1;update 学生信息 set 学号=&#39.ToString().ToString();
+ ((TextBox)(GridView1.ToString().Rows[e.RowIndex];
+ ((TextBox)(GridView1,姓名&#39.ToString();&#39.Rows[e.Cells[1].RowIndex].Rows[e;.Open().Controls[0]));&quot.ToString().Button”的对象强制转换为类型“System.Cells[2].Text.Rows[e.Trim() + &quot,就可以更新此条学生数据;';&;'
sqlcom.ToString().ToString()
麻烦各位了我把那段代码从Cells[2]依次修改到Cells[10]说详细点好吗?我具体应该怎么修改,它又说指定的参数已超出有效值的范围。。?我0基础。参数名: index我到底应该怎么办啊55我问的不是问题的原因,所以
提问者采纳
RowIndex].,你可以用Controls[参数]来选择哪一个控件。.Cells[1]你的更新语句 姓名字段少了个=号.这其中有一列中间是Button控件.... Cells[1]到Cells[9]分别代表第2列到第10列。 Cells是单元格的意思..Controls[0])这一句的意思是GridView更新行的第2列的第1个控件,Cells[1]也就是第2个单元格即第2列.,你检查一下.-------------------------GridView1,0就是1,参数从0开始.
你出现错误的原因可能是你的GridView里的Cells里有两个控件,比方说同时有TextBox和BUTTON,参数从0开始.Rows[e.
提问者评价
虽然问题没解决,但你的回答我学到的东西最多~只能给你了。。
其他类似问题
按默认排序
其他13条回答
你将它转换成数字;aaa&这样写肯定会出错的。int m=(int)(str),你偏要把它转换成 textbox当然就不行,当然会出错了;,就像是一个字符串是aaastring str=&quot就是说你的类型是button
楼主啊楼主 O(∩_∩)O
O(∩_∩)O O(∩_∩)O
O(∩_∩)O O(∩_∩)O
O(∩_∩)O O(∩_∩)O
O(∩_∩)O O(∩_∩)O
O(∩_∩)O 你试了吗?楼主你可以试着把GridView1.Rows[e.RowIndex].Cells[3].Controls[0]))换成 GridView1.Rows[e.RowIndex].Cells[1].FindControl[&控件的名称&] 看看,我不确定可不可以!不过表面上意思是一样的!
你这样的做法是非常不可取的,呵呵,你怎么能保证Controls[0]的控件就一定是TextBox呢?所以最好不要这样,想知道办法加偶的hi
你肯定哪个地方button=TextBox了,你发的那段代码根本就没有这样的错误。还有你这样写不是很好,用FindControl(&Control_id&)as ControlClass).text这样更好一点.
这样更新就是容易出错 .....controls[n],n 不一定是从0 开始 有时候从2开始不报错..1就报错..
下面是我写的一段代码,你对照着试试,因为我不知道你的是不是都是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& /&
&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)
//执行循环,保证每条数据都可以更新
for (i = -1; i & GridView1.Rows.C i++)
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
//当鼠标停留时更改背景色
e.Row.Attributes.Add(&onmouseover&, &c=this.style.backgroundCthis.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)
string inquireid = GridView1.DataKeys[e.RowIndex].Value.ToString();
string fixedassetid = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).T
string type = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl(&ddl1&)).SelectedItem.T
string softtype = &&;
if (type == &软件&)
softtype = ((DropDownList)GridView1.Rows[e.RowIndex].Cells[5].FindControl(&ddl2&)).SelectedItem.T
softtype = &&;
string brandorsoftname = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).T
string model = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).T
string configuration = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].Controls[0])).T
string servicetag = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].Controls[0])).T
string os = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[10].Controls[0])).T
string warranty = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[11].Controls[0])).T
string annualcost = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[12].Controls[0])).T
string purchasedate = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[13].Controls[0])).T
string price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[14].Controls[0])).T
string owncompany = (GridView1.Rows[e.RowIndex].Cells[15]).T
string usernumber = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[16].Controls[0])).T
string remark = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[17].Controls[0])).T
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]));
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();
Response.Write(&&script&alert('Please check the format!')&/script&&);
}protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
GridView1.EditIndex = e.NewEditI
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);
mandType = CommandType.StoredP
//添加参数
SqlParameter inquireId = new SqlParameter(&@inquireid&, SqlDbType.VarChar, 50);
inquireId.Value = P_Str_
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.V
((DropDownList)GridView1.Rows[e.NewEditIndex].Cells[5].FindControl(&ddl2&)).SelectedValue = HiddenField2.V
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();
+ &',姓名'& 这儿出问题了应该是 + &',姓名= '&
Controls[0]是TextBox,是Button才会报这样的错
你把列和行搞反了Cells〔列〕
我也是菜鸟不过你是怎么做的啊?控件是什么啊?
全部会显式转换就好了
报类型不能转换的错误,应该是你的单元格里的控件有类型是button,button的当然不能在程序中用textbox的类型来强制转换啊。另外,有效范围超出,是因为如果你的gridview如果只有10列,第10个单元格不是CELL[10]而是cell[9],记住,一般都是从0开始为第一个的。
把Control[]中的0改成1或是2就行了否则的话你还是把前台代码也贴出来吧,因为这样看,看不全面!
gridview的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 gridview控件属性 的文章

 

随机推荐