您的位置:程序门 -> .net技术 -> vb.net



还是datagrid的问题,烦了一天了,请高手赐教!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


还是datagrid的问题,烦了一天了,请高手赐教![已结贴,结贴人:zhybgy]
发表于:2007-02-01 14:32:31 楼主
有什么方法可以取得datagrid中的所有纪录数和所选中的纪录数,
用datagrid1.visiblerowcount只能取得当前可见的纪录数,请问还有什么方法可以实现啊?
发表于:2007-02-01 16:08:501楼 得分:10
datagrid中的数据是来自dataset吧?

用dataset名.表名.count就是记录数了。
发表于:2007-02-01 16:14:582楼 得分:10
ctype(datagrid1.datasource,datatable).rows.count
发表于:2007-02-01 16:35:033楼 得分:0
.net自带的datagrid好像不支持多选吧
用ctl选中多行以后,怎么得到选中了多少行啊
八斗兄现在不是在日本吗?还有时间光临啊?多谢了
发表于:2007-02-02 09:11:544楼 得分:10
作一个循环进行统计。
dim   i   as   int
dim   j   as   int
i   =   0  
j   =   0
for   i     to   datagrid中的记录数(记录数的得到如前面)  
    if   isselect(i)   =   true   代表已经选择了。
      j   =   j+1  
    endif
next

最后j   就是选择了的数字了。
发表于:2007-02-02 14:33:545楼 得分:0
isselect(i)   ?这个方法是哪里来的?
我有个想法,能不能通过每一行的颜色来判断是否被选中以及计算出有多少行被选中
但是好像取不到行的显示颜色啊
大家谁有好一点的想法告诉一声啊
多谢了!
发表于:2007-02-02 16:04:506楼 得分:0
dim   mydatarow   as   datarow
                dim   mytable2   as   datatable
                dim   rcount   as   integer,   i   as   integer,   j   as   integer
                dim   mycmd   as   new   oledbcommand


                mytable1   =   myset.tables(0)                     '如果该货架号没有数据则不会进行删除
                if   mytable1.rows.count   =   0   then
                        exit   sub
                end   if

                mytable2   =   mytable1.clone                       '在数据集中生成第二张表以存放table1中标注为删除行的数据
                mytable2.tablename   =   "table2 "
                mytable2.clear()
                if   myset.tables.count   <   2   then
                        myset.tables.add(mytable2)
                end   if

                rcount   =   myset.tables(0).rows.count   -   1
                record   =   new   integer(rcount)   {}

                for   i   =   0   to   rcount                                                             '获取dayagrid控件中被选中的单元行的行号
                        record(i)   =   -1
                        if   datagr.isselected(i)   =   true   then
                                record(i)   =   i
                        end   if
                next
                dim   k   as   integer
                for   i   =   0   to   rcount
                        mydatarow   =   mytable2.newrow
                        if   record(i)   >   -1   then
                                for   j   =   0   to   mytable1.columns.count   -   1
                                        mydatarow(j)   =   mytable1.rows(i).item(j)
                                next
                                mytable2.rows.add(mydatarow)
                                mytable1.rows(i).delete()
                                k   =   k   +   1
                        end   if
                next
                if   k   =   0   then
                        exit   sub
                end   if
                if   messagebox.show( "有 "   &   k.tostring   &   "条数据被选中 "   _
                  &   vbcrlf   &   vbcrlf   &   "确定要删除选中的数据吗? "   &   vbcrlf   &   vbcrlf   &   "选择 '是 ',则数据将无法恢复 ",   me.text,   messageboxbuttons.yesno,   messageboxicon.warning)   =   dialogresult.yes   then
                        mytable1.acceptchanges()
                        if   mytable2.rows.count   >   0   then
                                with   mycmd
                                        .commandtext   =   "delview "
                                        .commandtype   =   commandtype.storedprocedure
                                        .connection   =   con
                                end   with
                                for   i   =   0   to   mytable2.rows.count   -   1
                                        dim   pitem   as   oledbparameter   =   mycmd.parameters.add( "@item ",   oledbtype.longvarchar)
                                        dim   pupc   as   oledbparameter   =   mycmd.parameters.add( "@upc ",   oledbtype.longvarchar)
                                        dim   pitemname   as   oledbparameter   =   mycmd.parameters.add( "@itemname ",   oledbtype.longvarchar)
                                        dim   pguig   as   oledbparameter   =   mycmd.parameters.add( "@guig ",   oledbtype.longvarchar)
                                        dim   pea   as   oledbparameter   =   mycmd.parameters.add( "@ea ",   oledbtype.longvarchar)
                                        dim   pnumber   as   oledbparameter   =   mycmd.parameters.add( "@number ",   oledbtype.longvarchar)
                                        dim   parea   as   oledbparameter   =   mycmd.parameters.add( "@area ",   oledbtype.longvarchar)
                                        dim   pscanno   as   oledbparameter   =   mycmd.parameters.add( "@scanno ",   oledbtype.integer)

                                        pitem.value   =   mytable2.rows(i)(1).tostring
                                        pupc.value   =   mytable2.rows(i)(2).tostring
                                        pitemname.value   =   mytable2.rows(i)(3).tostring
                                        pguig.value   =   mytable2.rows(i)(4).tostring
                                        pea.value   =   mytable2.rows(i)(5).tostring
                                        pnumber.value   =   combobox2.text
                                        parea.value   =   combobox1.text
                                        pscanno.value   =   mytable2.rows(i)(0).tostring
                                        mycmd.EXECutenonquery()
                                        mycmd.parameters.clear()
                                next
                        end   if
                else
                        for   each   mydatarow   in   mytable1.rows
                                if   mydatarow.rowstate   =   datarowstate.deleted   then
                                        mydatarow.rejectchanges()
                                end   if
                        next
                end   if
发表于:2007-02-05 00:05:157楼 得分:0
‘下面代码已测试通过
private   cm   as   currencymanager
        private   sub   proporty_employee_load(byval   sender   as   system.object,   byval   e   as   system.eventargs)   handles   mybase.load
                binddata()                     '数据绑定
        end   sub

        private   sub   binddata()
                try
                        dim   sql   as   string   =   "select   *   from   employee "
                        dim   dt   as   datatable   =   getdatatable(sql)
                        if   dt.rows.count   <>   0   then
                                me.datagrid1.datasource   =   dt
                                cm   =   me.bindingcontext(dt)
                                me.lbl记录数.text   =   string.format( "当前第{0}条,共{1}条记录! ",   cm.position   +   1,   cm.count)
                        end   if
                catch   ex   as   exception
                        messagebox.show(ex.message)
                end   try
        end   sub

'返回datatable
  public   function   getdatatable(byval   sql   as   string)   as   datatable
                dim   dt   as   new   datatable
                try
                        con.open()
                        dim   sqlada   as   new   sqldataadapter(new   sqlcommand(sql,   con))
                        sqlada.fill(dt)
                        con.close()
                catch   ex   as   exception
                        messagebox.show(ex.message)
                end   try
                return   dt
        end   function

'datagrid控件记录变化
private   sub   datagrid1_currentcellchanged(byval   sender   as   object,   byval   e   as   system.eventargs)   handles   datagrid1.currentcellchanged
                me.lbl记录数.text   =   string.format( "当前第{0}条,共{1}条记录! ",   cm.position   +   1,   cm.count)
        end   sub
发表于:2007-02-05 00:29:248楼 得分:0
datagrid1.rows.count   即可得datagrid1的记录数
datagrid1.selectedrows.count     即可得选中的记录数

这都是datagridview的自带方法啊.


快速检索

最新资讯
热门点击