数据库中有两个数据“烧饼”,“馒头”,如何给他们邦定gridview后给他们一个排序也就是说让其馒头的都在一起,烧饼都在一起。如:
馒头
馒头
馒头
馒头
合计:*个
烧饼
烧饼
烧饼
烧饼
合计:*个
其2:如何当判断馒头读取完后,自动插入一行计算出馒头的个数阿?
拜托各位了!

解决方案 »

  1.   

    如果是使用的是SQL Server,建議直接在Sql查詢中統計好即可.
    否則的話,自己手工操作DataSource並增加相應的列。
      

  2.   

    gridview在html中解晰为table
    可以考虑在客户端做
    但需要先排好序var OldGroup='';
    var NewGroup=''
    var GruopColIndex=-1;
    var GroupSumConIndex=-1;
    var GroupSum=0;
    var XmlReq;
    var OriObj;
    function LoadSQL(SQL,Obj)
      {
         ShowLoading(1);
         AspxServer="GetGroup.aspx";     
         OriObj=Obj; 
         SQL=escape(SQL); 
         var dt=new Date();
          var requestUrl = AspxServer+'?SQL='+SQL+'&tmp='+dt.getTime();
          CreateXmlReq();
          if(XmlReq)
         {       
        XmlReq.onreadystatechange = HandleResponseSQL;
        XmlReq.open("GET", requestUrl,  true);
        XmlReq.send();
       }
    }function CreateXmlReq()
      {
    try
    {
    XmlReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
    try
    {
    XmlReq = new ActiveXObject("Microsoft.XMLHTTP");

    catch(oc)
    {
    XmlReq = null;
    }
    }
    if(!XmlReq && typeof XMLHttpRequest != "undefined") 
    {
    XmlReq = new XMLHttpRequest();
    }
    }
    function HandleResponseSQL()
    {
    if(XmlReq.readyState == 4)
    {
    if(XmlReq.status == 200)
    {                       
         GroupObj(XmlReq.responseXML.documentElement);
          ShowLoading(0);    
    }
    else
    {
    alert("从服务器读取数据失败,请与电脑部联系!" );
    window.status="完成";
    ShowLoading(0);
    }
    }
    }function GroupObj(Xml)
     {
        var auth = Xml.getElementsByTagName('Group'); 
       var tb=document.getElementById(OriObj);
        var ColCount=tb.rows[0].cells.length;//得到总列数
        var RowCount=tb.rows.length-1;//得到总行数    
        for(var j=0;j<ColCount;j++)
          {
            if(tb.rows[0].cells[j].innerText==auth.context.childNodes(0).childNodes(0).nodeName) 
              {
                 GruopColIndex=j;//得到要分组的列值                     
            }
            if(tb.rows[0].cells[j].innerText==auth.context.childNodes(0).childNodes(1).nodeName) 
              {  
                GroupSumConIndex=j;
            }    
        }   
        OldGroup=tb.rows[1].cells[GruopColIndex].innerText;//初始值  
        for(var j=1;j<RowCount+1;j++)
         {
            for(var i=0;i<auth.context.childNodes(0).parentNode.childNodes.length;i++)
              {
                 if(auth.context.childNodes(i).childNodes(0).text==OldGroup) 
                   {
                      GroupSum=auth.context.childNodes(i).childNodes(1).text;               
                 }  
              
            }   
            if(tb.rows[j].cells[GruopColIndex].innerText!=OldGroup)// & tb.rows[j].cells[0].innerText!=''  )//如果第下一行要分组的值不等于一行的值,那么在此行前面插入一行放入分组信息
              {
                  var Row=tb.insertRow(j); //插入行
                 AddRow(Row,OriObj,ColCount,RowCount,1);  
                 Row.className='GroupRow'; 
                 Row.cells[GroupSumConIndex].innerText=GroupSum;
                 Row.cells[GruopColIndex].innerText=OldGroup+'合計:';
                 RowCount=RowCount+1;
                 OldGroup=tb.rows[j+1].cells[GruopColIndex].innerText;//重新定义分组资料   
                 if(j==tb.rows.length-1)
                   {
                     return false;
                 }  
               }          
           else
            {
            
            if(j==tb.rows.length -1)
              {
                 var Row=tb.insertRow(-1); //在表格最後行追加一行    
                 AddRow(Row,OriObj,ColCount,RowCount,1);  
                 Row.className='GroupRow'; 
                 Row.cells[GroupSumConIndex].innerText=GroupSum;
                 Row.cells[GruopColIndex].innerText=OldGroup+'合計:';
                 ReSetRowID(OriObj);
            }
          } 
       }   
    }
    function AddRow(Row,ParentID,ColCount,RowCount,Group)
     {
         Row.className="relativeTag";
        for(i=0;i<ColCount;i++)//插入列
          {
             var Col = Row.insertCell(i);
             var OldHtml=document.getElementById(ParentID).rows[RowCount].cells[i].innerHTML;
             OldHtml=OldHtml.replace(/"class=itemCellStyle"/g,"class=itemCellStyle type='text'"); //加入html type=text
             if(OldHtml.indexOf('value')>0 && OldHtml.indexOf('INPUT')>0) //
               {
                 OldHtml=OldHtml.replace(OldHtml.substring(OldHtml.indexOf('value'),OldHtml.indexOf('name')),'' );//将value=??? 替换为空
             }
             Col.innerHTML=OldHtml;
             if(i==0)
               {
                  if(Group==0) //如果不是分組行
                    {
                      Col.innerText=RowCount+1;
                      Col.onclick=function()
                       {
                         OnKeyDel(ParentID,this,0);
                      }
                  }    
                  Col.className='fixedHeaderCol';
               }
             else 
               {           
                 if(Col.innerHTML.indexOf("class=itemCellStyle")>0)//重设client id
                    {
                      Oldid=Col.children[0].id;
                      Oldname=Col.children[0].name;
                      Col.children[0].id=Oldid.replace(RowCount,RowCount+1);//重设id
                      Col.children[0].name=Oldname.replace(RowCount,RowCount+1);//重设name
                      //ReSetColID(RowCount,Oldid,Oldname,RowCount+1,Col);  
                  }
                  if(Col.innerHTML.indexOf('class')<0)//如果在datagrid没有设成textbox,那么将其值清为空
                    {
                      Col.innerText="";
                   }   
              }           
        }
     }
      

  3.   

    在服务器端调用客户端事件LoadSQL
    参数说明:
    SQL:分组的sql,例如:select type,count(*) count from table group by type
    Obj:dataview在客户端的id
      

  4.   

    少了个js function 
    //顯示正在加載中圖片
    function ShowLoading(Show)
     {
       var b=document.getElementById("DivShowLoad");
       if(Show==0)//不顯示
         {
            if(b!=null)
              {
                 //document.body.removeChild(document.getElementById("DivShowLoad"));
                 document.getElementById("DivShowLoad").style.display='none';
            }
          }
       else  //顯示
         {
            if(b==null)  //如果不存在DivShowLoad,则创建
              { 
                var NewDiv="<div id='DivShowLoad' class='div'></div>";
                document.body.insertAdjacentHTML("beforeEnd",NewDiv);
              }
            else
              {
                document.getElementById("DivShowLoad").style.display='';
             }
         }
     }
     
    //以上為顯示正在加載中圖片
      

  5.   

    Css
    .div 
       { 
      position:absolute; 
      top:50%; 
      left:50%; 
      margin:-100px   0   0   -100px; 
     width:100px; 
     height:100px; 
     Z-INDEX:9999999;
     CURSOR:hand;
     background-image:url('login_loader_01.gif');   
    } .GroupRow
     {
       background-color:Gray; 

     .relativeTag
        { 
        position:relative;
       }
    需要在html中引用
      

  6.   

    直接写在 GridView的Footer里也可以
      

  7.   

    还有个服务器端的Cs忘了给
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    string SQL = Server.UrlDecode(Page.Request.QueryString["SQL"].ToString());
                    Response.Clear();
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = "server=(local);database=test;uid=sa;pwd=";
                    Conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(SQL,Conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "table");
                    da.Dispose();
                    Conn.Close();
                    Conn.Dispose();
                    ds.DataSetName = "Group"; 
                    string chString = ds.GetXml();
                    Response.Clear();
                    Response.ContentType = "text/xml";
                    Response.Write(chString);
                    Response.End();
                }
                else
                {
                    Response.Clear();
                    Response.End();
                }
            }