DataGrid操作的一些总结

1.DataGrid动态绑定字段

      有时候我们会遇到在DataGrid中动态绑定某个字段的情况,比如:当显示中文时我们绑定cname,显示英文时我们要绑定ename,此时我们可以这样:
ButtonColumn:
if (language == "cn")
     ((ButtonColumn)this.DataGrid1.Columns[3]).DataTextField = "cname";
else
     ((ButtonColumn)this.DataGrid1.Columns[3]).DataTextField = "ename";

BoundColumn:
((BoundColumn)this.DataGrid1.Columns[3]).DataField = "cname";

HyperLinkColumn同ButtonColumn的写法,不同的是当我们要动态改变TemplateColumn的显示应该怎么办呢?
这样就行了 先新建一个模板:
public class Template1 :ITemplate
 {
    public void InstantiateIn(Control container)
    {
        TextBox txt = new TextBox();
       txt.Text = "this is a added template column";
       container.Controls.Add(txt);
     }
 }
再这样写:
((TemplateColumn)this.DataGrid1.Columns[1]).ItemTemplate = new Template1();

2.动态转换DataGrid中某个Cell中的值

BoundColumn:
if (e.Item.Cells[3].Text == "Ann")
     e.Item.Cells[3].Text = "<b>这一行本来应该显示Ann,现在被转换成这样了!</b>";

ButtonColumn :
LinkButton link = (LinkButton)e.Item.Cells[3].Controls[0];
if (link.Text == "Ann")
     link.Text = "<b>这一行本来应该显示Ann,现在被转换成这样了!</b>";
这段代码写在:OnItemDataBound事件的方法体中就可以了.

HyperLinkColumn:
强类型转换成HyperLink,

Template Column:
通过FindControl方法

3.动态创建DataGrid及添加列

DataGrid dg = new DataGrid();
    
    // BoundColumn
    BoundColumn col1 = new BoundColumn();
    col1.DataField = "au_id";
    col1.HeaderText = "au_id";
    dg.Columns.Add(col1);

    // ButtonColumn
    ButtonColumn col2 = new ButtonColumn();
    col2.DataTextField = "au_fname";
    col2.HeaderText = "au_fname";
    dg.Columns.Add(col2);

    // TemplateColumn
    TemplateColumn col3 = new TemplateColumn();
    col3.ItemTemplate = new Templates.Template1();
    col3.HeaderText = "My Template Column";
    dg.Columns.Add(col3);

    // LinkButtonColumn
    HyperLinkColumn col4 = new HyperLinkColumn();
    col4.HeaderText = "au_lname";
    col4.DataTextField = "au_lname";
    col4.DataNavigateUrlField = "au_lname";    
    dg.Columns.AddAt(0,col4);
    
    SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=;database=pubs"); 
    SqlDataAdapter da = new SqlDataAdapter("select * from authors",conn);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dg.DataSource = ds;
    dg.DataBind();
    Page.Controls[1].Controls.Add(dg);

4.关于.Net中将DataGrid内容导出到Excel乱码的问题
一般常用的方法为:
DataGrid1.DataSource = this.GetDataSource();   
DataGrid1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.ContentType            = "application/vnd.ms-excel";
Response.Charset                    = "gb2312";
EnableViewState                     = false;
System.IO.StringWriter tw       = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid1.RenderControl(hw );
Response.Write(tw.ToString());
Response.End();

但是有时导出会出现乱码,有时则不会出现乱码.真是百思不得其解.

我们可以这样解决将
Response.ContentType            = "application/vnd.ms-excel";
Response.Charset                    = "gb2312";
换成:
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
Response.AppendHeader("content-disposition","attachment;filename=\"" + HttpUtility.UrlEncode("全部销售记录["+DateTime.Now.ToString("yyyy-MM-dd")+"]",System.Text.Encoding.UTF8) + ".xls\"");

为什么直接输出到Excel会出现乱码,而用Excel打开这段Html不会出现乱码呢?暂时能解决问题但还是不明白,希望高手能给予释疑!

你可能感兴趣的:(datagrid)