百度首页 | 百度空间
 
文章列表
 
您正在查看 "Gridview" 分类下的文章

2007-08-22 15:54

Here is the complete code to Export GridView to Excel:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class ExportGridView : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
      {
        
if (!Page.IsPostBack)
          {
              GridView1.DataSource = BindData();
              GridView1.DataBind();
          }
      }

    

    
private string ConnectionString
      {

        
get { return @"Server=localhost;Database=Northwind;
          Trusted_Connection=true"; }

      }

    

    
private DataSet BindData()
      {
        
// make the query
        
string query = "SELECT * FROM Categories";
          SqlConnection myConnection =
new SqlConnection(ConnectionString);
          SqlDataAdapter ad =
new SqlDataAdapter(query, myConnection);
          DataSet ds =
new DataSet();
          ad.Fill(ds, "Categories");
        
return ds;

      }




    
protected void Button1_Click(object sender, EventArgs e)
      {
          Response.Clear();

          Response.AddHeader("content-disposition", "attachment;
          filename=FileName.xls");

          Response.Charset = "";

        
// If you want the option to open the Excel file without saving than

          // comment out the line below

          // Response.Cache.SetCacheability(HttpCacheability.NoCache);

        
Response.ContentType = "application/vnd.xls";

          System.IO.StringWriter stringWrite =
new System.IO.StringWriter();

          System.Web.UI.HtmlTextWriter htmlWrite =
        
new HtmlTextWriter(stringWrite);

          GridView1.RenderControl(htmlWrite);

          Response.Write(stringWrite.ToString());

          Response.End();

      }

    
public override void VerifyRenderingInServerForm(Control control)
      {

        
// Confirms that an HtmlForm control is rendered for the
        
specified ASP.NET server control at run time.

      }
}

powered by IMHO

另外一种方法参看:http://www.cnblogs.com/sparon/archive/2007/04/06/702946.html

类别:Gridview | 评论(0) | 浏览()
 
2007-04-24 15:55
在ASP.Net中对ObjectDataSource自动配置数据源的[删除]操作的时候,会生成两个字段一个是

OldValuesParameterFormatString="original_{0}"

另外一个是

<DeleteParameters>
                                 <asp:Parameter Name="original_XML_ID" Type="Int32" />
                             </DeleteParameters>

这里面OldValuesParameterFormatString是根据你设定的SQL语句中的参数确定的,而Parameter Name却是根据你的业务罗基层中删除函数的第一个参数确定的。
比如,业务逻辑层中你的删除组件是这样定义的,函数中的第一个参数就是ObjectDataSource生成的Parameter Name

     [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
     public bool DelXML(int original_XML_ID)
     {
         int rowsAffected = Adapter.Delete(original_XML_ID);

         return rowsAffected == 1;
     }


这里要注意的是你BLL中DEL函数中的括弧中的参数名称和SQL中的参数名称如果不一样,则会出现“未能找到带参数的非泛型方法”得奇怪错误。不知道算不算是ASP.NET的BUG不过确实很讨厌。就连微软官方的教程(见www.asp.net)中都没有解决这个BUG。
Original_ID
类别:Gridview | 评论(0) | 浏览()
 
2007-04-23 21:54

转帖的


ASP.NET的GridView控件允许你通过设置它的EditIndex属性来编辑数据行,此时整个数据行都处于编辑模式。 如果你在EditItemTemplate的一些列中使用了DropDownList控件,那么你也许不希望整个数据行都处于编辑模式。 因为,如果每一个DropDownList控件都有很多选项的话,那么一次加载所有DropDownList控件的所有选项就会导致页面执行缓慢。

另外,如果你的数据行的编辑模式需要占用更多的空间的话,那么针对每一个独立的单元格进行编辑要优于针对整个数据行进行编辑。 这里,我将示范如何实现这样的功能,又如何去处理事件验证(event validation)。


背景
本文基于我之前写的一篇文章:GridView和DataList响应单击数据行和双击数据行事件。如果你不知道如何让GridView响应单击数据行事件,那么你可以在阅读本文之前先看看这篇文章。


编辑某一个独立的GridView单元格。

主要是mark下学习资源,更多内容查看http://www.cnblogs.com/webabcd/archive/2007/04/22/723113.html

原文地址:http://www.codeproject.com/useritems/EditGridviewCells.asp
[原文源码下载]
[译者改后源码下载]


[翻译]在GridView中针对鼠标单击的某一独立单元格进行编辑



原文发布日期:2007.04.07
作者:Declan Bright
翻译:webabcd

类别:Gridview | 评论(0) | 浏览()
 
2007-04-18 03:11

关于Gridview的自定义显示字段有多种方法

第一种就请看这里,字段写起来比较长一点感觉有点麻烦

这里讲一个另外一种,也是比较方便的一种办法

比如说我现在有两张表

一张表是 Detail, 一张是Sheet表。其中Detail 表中有行 SheetID是关联Sheet表的ID字段。

这样我在显示Detail表的GridView的时候希望显示的是SheetID的这个ID的SheetName。

ok,我是按照类的办法来做的,建立了一个BLL类,名字叫做 SheetBLL,其中可以通过 SheeBLL.GetName(int nID)返回一个string的表名(其实就是一个查询过程啦)

第一步,在给Detail表的ID字段转换为ItemTemplate,然后在Label绑定的时候 自定义绑定格式,输入

GetName(Eval("SheetID"))

第二步,在后置文件CS里面建立如下,这里通过查询nSheetID返回得到了sheet的值

      public string GetName(object SheetID)
      {
          int nSheetID = Convert.ToInt32(SheetID.ToString());

          SheetBLL sheetbll = new SheetBLL();

         return sheetbll.GetName(nSheetID );
         }

类别:Gridview | 评论(0) | 浏览()
 
2007-04-17 14:14

一个项目希望Gridview点击弹出一个window.open,或者是showModalDialog的窗口。

下面使用showModalDialog的东西,感觉还是ShowModalDialog比较好一点

方法1 绑定cell里面的具体控件

if (e.Row.Cells[0].Controls.Count > 1)

{

HyperLink zlluClick = (HyperLink)e.Row.Cells[0].FindControl("HyperLink1");

HiddenField zlluHidden = (HiddenField)e.Row.Cells[0].FindControl("HiddenField1"); zlluClick.Attributes.Add("onclick", "window.showModalDialog('ShowDetail.aspx?id=" + zlluHidden.Value + "','','width=300,height=300,toolbar=no,menubar=no,scrollbars=no')");

}

2.还有一种绑定整个个cell的方法,感觉这个方法更加的好
            e.Row.Cells[3].Attributes.Add("onclick", "window.showModalDialog('../Budget/DetailList.aspx?SheetID=" +detail.Budget_Sheet_ID + "','','width=300,height=300,toolbar=no,menubar=no,scrollbars=no')");

类别:Gridview | 评论(0) | 浏览()
 
2007-04-15 21:51
 给Delete列添加确认对话框最简单的方法是把CommandField的DeleteText属性设置为:
<div onclick="JavaScript:return confirm('Are you sure to delete?')">Delete</div>

加入div后会产生换行效果,若不想要换行效果可以使用span:

<span onclick="JavaScript:return confirm('Are you sure to delete?')">Delete</span>

http://www.cnblogs.com/doraeimo/archive/2007/01/01/609344.html

类别:Gridview | 评论(0) | 浏览()
 
2007-04-13 02:00

在使用C#写CRM时遇到一个问题,GridView绑定了了数据后,我想在鼠标停留到GridView数据行时,行变色突出显示该行,并且鼠标点击该行时,将该条目的数据显示在GridView下面的控件自动将该行对应的数据显示出来。

我的实现步骤是:

1.增加GridView的GVSelect_RowDataBound事件

protected void GVSelect_RowDataBound(object sender, GridViewRowEventArgs e)

       {

           if (e.Row.RowType == DataControlRowType.DataRow)

           {

               //当鼠标停留时更改背景色

               e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#8EC26F'");

               //当鼠标移开时还原背景色

               e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

               //设置悬浮鼠标指针形状为"小手"

               e.Row.Attributes["style"] = "Cursor:hand";

               //单击/双击 事件

               e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[0].Text + "')");

               //注:OnClick参数是指明为鼠标单击时间,后个是调用javascript的ClickEvent函数

           }

}

2.在页面的HTML里添加javascript函数,用来响应鼠标点击事件,因为ASP客户端不能主动调用服务端的函数,我在这里添加一个LinkButton,将其text设置为空,然后在ClickEvent(cId)函数中,调用这个LinkButton的单击事件。

<script language="javascript">

       function ClickEvent(cId)

       {

           //调用LinkButton的单击事件,btnBindData是LinkButton的ID

           document.getElementById("btnBindData").click();

       }

</script>

3.添加LinkButton的响应事件

      protected void btnBindData_Click(object sender, EventArgs e)

{

            //在这里对你需要的数据信息进行输出

            SetClientInfo();//我的处理函数

}

GridView鼠标停留变色和单击处理事件,当鼠标在GridView的行上停留时,将该行变色,当单击该行时,做相应处理

另外一种Gridview鼠标上浮,行变色的办法如下,其实就是增加一个onmouseover,这个感觉更加容易理解一点,而且也容易读代码,还是推荐这个

if (e.Row.RowType == DataControlRowType.DataRow)
         {
             //鼠标经过时,行背景色变
             e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
             //鼠标移出时,行背景色变
             e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
         }

     }

类别:Gridview | 评论(0) | 浏览()
 
     
 
 
文章分类
 
 
 
 
 
 
 
 
Ajax(5)
 
 
 
 
 
 
 
 
 
     
 
文章存档
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
     
 
最新文章评论
   

谢谢分享。
 

thank you very much,you are good man
 
 

 

不错,挺好看的。顶了。
 
     


©2008 Baidu