本文整理汇总了C#中System.Web.UI.WebControls.DataGrid.DataBind方法的典型用法代码示例。如果您正苦于以下问题:C# DataGrid.DataBind方法的具体用法?C# DataGrid.DataBind怎么用?C# DataGrid.DataBind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.UI.WebControls.DataGrid
的用法示例。
在下文中一共展示了DataGrid.DataBind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//if (Session["Type"].ToString() == "Excel")
//{
DataSet ds = Session["ExcelDs"] as DataSet;
Response.Clear();
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=ExcelName.xls");
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
//}
//else
//{
// Response.ContentType = "application/pdf";
// string path = Server.MapPath("~/PDFS/TestM10.pdf");
// byte[] bts = System.IO.File.ReadAllBytes(path);
// Response.Clear();
// Response.ClearHeaders();
// Response.AddHeader("Content-Type", "Application/octet-stream");//octet-stream
// Response.AddHeader("Content-Length", bts.Length.ToString());
// Response.AddHeader("Content-Disposition", "attachment; filename=1.pdf");
// Response.BinaryWrite(bts);
// Response.Flush();
// Response.TransmitFile(path);
// Response.End();
//}
}
示例2: ExecuteResult
public override void ExecuteResult(ControllerContext context)
{
var r = context.HttpContext.Response;
r.Clear();
r.ContentType = "application/vnd.ms-excel";
if (!string.IsNullOrEmpty(FileName))
r.AddHeader("content-disposition",
"attachment;filename=" + FileName);
const string header =
@"<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">
<head>
<meta http-equiv=Content-Type content=""text/html; charset=utf-8"">
<style>
<!--table
br {mso-data-placement:same-cell;}
tr {vertical-align:top;}
-->
</style>
</head>
<body>";
r.Write(header);
r.Charset = "";
var dg = new DataGrid();
dg.EnableViewState = false;
dg.DataSource = Data;
dg.DataBind();
dg.RenderControl(new HtmlTextWriter(r.Output));
r.Write("</body></HTML>");
}
示例3: ExecuteResult
public override void ExecuteResult(ControllerContext context)
{
var Response = context.HttpContext.Response;
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
var dg = new DataGrid();
string filename = null;
switch (type)
{
case "donorfundtotals":
filename = "DonorFundTotals";
dg.DataSource = ExportPeople.ExcelDonorFundTotals(Dt1, Dt2, fundid, campusid, pledges, nontaxdeductible, IncUnclosedBundles);
break;
case "donortotals":
filename = "DonorTotals";
dg.DataSource = ExportPeople.ExcelDonorTotals(Dt1, Dt2, campusid, pledges, nontaxdeductible, IncUnclosedBundles);
break;
case "donordetails":
filename = "DonorDetails";
dg.DataSource = ExportPeople.DonorDetails(Dt1, Dt2, fundid, campusid, pledges, nontaxdeductible, IncUnclosedBundles);
break;
}
dg.DataBind();
Response.AddHeader("Content-Disposition", "attachment;filename={0}.xls".Fmt(filename));
dg.RenderControl(new HtmlTextWriter(Response.Output));
}
示例4: UploadDataTableToExcel
private void UploadDataTableToExcel(DataTable feedBacks)
{
HttpResponse response = HttpContext.Current.Response;
try
{
response.Clear();
response.Charset = string.Empty;
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"FeedBack.xls\"");
using (StringWriter stringWriter = new StringWriter())
{
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
DataGrid dataGrid = new DataGrid();
dataGrid.DataSource = feedBacks;
dataGrid.DataBind();
dataGrid.RenderControl(htmlTextWriter);
response.Write(stringWriter.ToString());
}
}
response.End();
}
catch (ThreadAbortException ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
}
示例5: ExecuteResult
public override void ExecuteResult(ControllerContext context)
{
var Response = context.HttpContext.Response;
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=CMSPeople.xls");
Response.Charset = "";
var name = "ExtraExcelResult " + DateTime.Now;
var tag = DbUtil.Db.PopulateSpecialTag(qid, DbUtil.TagTypeId_ExtraValues);
var roles = CMSRoleProvider.provider.GetRolesForUser(Util.UserName);
var xml = XDocument.Parse(DbUtil.Db.Content("StandardExtraValues.xml", "<Fields/>"));
var fields = (from ff in xml.Root.Elements("Field")
let vroles = ff.Attribute("VisibilityRoles")
where vroles != null && (vroles.Value.Split(',').All(rr => !roles.Contains(rr)))
select ff.Attribute("name").Value);
var nodisplaycols = string.Join("|", fields);
var cmd = new SqlCommand("dbo.ExtraValues @p1, @p2, @p3");
cmd.Parameters.AddWithValue("@p1", tag.Id);
cmd.Parameters.AddWithValue("@p2", "");
cmd.Parameters.AddWithValue("@p3", nodisplaycols);
cmd.Connection = new SqlConnection(Util.ConnectionString);
cmd.Connection.Open();
var dg = new DataGrid();
dg.DataSource = cmd.ExecuteReader();
dg.DataBind();
dg.RenderControl(new HtmlTextWriter(Response.Output));
}
示例6: ExportToExcelFile
/// <summary>
/// Exporta la información a Excel.
/// </summary>
/// <param name="response">HttpResponse actual.</param>
/// <param name="data">Datos a exportar.</param>
/// <param name="nombreArchivo">Nombre del archivo Excel</param>
public static void ExportToExcelFile(HttpResponse response, DataView data, string nombreArchivo)
{
var dg = new DataGrid { DataSource = data };
dg.DataBind();
response.Clear();
response.Buffer = true;
//application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
response.AddHeader("Content-Disposition", "filename=" + nombreArchivo);
response.ContentType = "application/vnd.ms-excel";
response.Charset = "UTF-8";
response.ContentEncoding = System.Text.Encoding.Default;
var stringWriter = new StringWriter();
var htmlWriter = new HtmlTextWriter(stringWriter);
dg.RenderControl(htmlWriter);
response.Write(stringWriter.ToString());
//resp.Flush();
try
{
response.End();
}
catch (Exception ex)
{
ISException.RegisterExcepcion(ex);
throw ex;
}
}
示例7: ExportToExcel
public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "Test1234.xls";
string excelHeader = "Quiz Report";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
// Report Header
hw.WriteLine("<b><u><font size=’3′> " + excelHeader + " </font></u></b>");
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
示例8: Convert
public static void Convert(System.Data.DataSet ds, int TableIndex, System.Web.HttpResponse response, string ExcelName)
{
//lets make sure a table actually exists at the passed in value
//if it is not call the base method
if (TableIndex > ds.Tables.Count - 1)
{
Convert(ds, response, ExcelName);
}
//we've got a good table so
//let's clean up the response.object
response.Clear();
response.Charset = "";
response.AddHeader("Content-Disposition", "attachment;filename=Shilpa.xls");
//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
//create an htmltextwriter which uses the stringwriter
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
//instantiate a datagrid
System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
//set the datagrid datasource to the dataset passed in
dg.DataSource = ds.Tables[TableIndex];
//bind the datagrid
dg.DataBind();
//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);
//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();
}
示例9: DataTable2Excel
/// <summary>
/// 导出Excel文件,转换为可读模式
/// </summary>
public static void DataTable2Excel(System.Data.DataTable dtData)
{
DataGrid dgExport = null;
HttpContext curContext = HttpContext.Current;
StringWriter strWriter = null;
HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = "";
strWriter = new StringWriter();
htmlWriter = new HtmlTextWriter(strWriter);
dgExport = new DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
try
{
dgExport.RenderControl(htmlWriter);
}
catch (Exception e)
{
Log4Net.LogWrite("err", e.Message);
}
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
示例10: save
private void save(DataTable dt,string filename)
{
DataGrid excel = new DataGrid();
System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
headerStyle.BackColor = System.Drawing.Color.LightGray;
headerStyle.Font.Bold = true;
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;
excel.AlternatingItemStyle.MergeWith(AlternatingStyle);
excel.HeaderStyle.MergeWith(headerStyle);
excel.ItemStyle.MergeWith(itemStyle);
excel.GridLines = GridLines.Both;
excel.HeaderStyle.Font.Bold = true;
DataSet ds = new DataSet();
ds.Tables.Add(dt);
excel.DataSource = ds; //输出DataTable的内容
excel.DataBind();
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
excel.RenderControl(oHtmlTextWriter);
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(oHtmlTextWriter.InnerWriter.ToString());
Response.End();
}
示例11: AssociarDadosGrid
protected void AssociarDadosGrid(DataGrid dgview, DataTable dtdados, Label lblresultado)
{
if (dtdados.Rows.Count > 0)
{
lblresultado.Visible = false;
dgview.DataSource = dtdados;
dgview.DataBind();
}
else
{
lblresultado.Visible = true;
lblresultado.Text = "Nenhum registro encontrado";
dgview.DataSource = null;
dgview.DataBind();
}
}
示例12: ToExcel
public void ToExcel(object sender, System.EventArgs e)
{
try
{
DataTable dtData = GetDTList();
System.Web.UI.WebControls.DataGrid dgExport = null;
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null && dtData.Rows.Count > 0)
{
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
curContext.Response.Charset = "UTF-8";
curContext.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312 >");
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
for (int i = 0; i < dgExport.Items.Count; i++)
{
for (int j = 0; j < 3; j++)
{
dgExport.Items[i].Cells[j].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
}
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
WriteAlertInfo(AlertSuccess);
}
else
{
WriteAlertInfo(AlertNoData);
}
}
catch (FisException fe)
{
this.GridViewExt1.DataSource = GetTable();
this.GridViewExt1.DataBind();
writeToAlertMessage(fe.mErrmsg);
}
catch (Exception ex)
{
this.GridViewExt1.DataSource = GetTable();
this.GridViewExt1.DataBind();
writeToAlertMessage(ex.Message);
}
finally
{
endWaitingCoverDiv();
}
}
示例13: Bind
public static void Bind(DataTable dt, DataGrid dg)
{
if (dt != null)
{
dg.DataSource = dt;
dg.DataBind();
}
}
示例14: BindAndSort
/// <summary>
/// Bind datagrid with sorting dataview.
/// </summary>
/// <param name="dv">DataView</param>
/// <param name="dataGrid">DataGrid</param>
/// <param name="sortField">Sorting Field</param>
/// <param name="keyField">DataGrid's Key Field</param>
public static void BindAndSort(DataView dv,DataGrid dataGrid,string sortField,string keyField)
{
if(dv != null)
dv.Sort = sortField;
dataGrid.DataSource = dv;
dataGrid.DataKeyField=keyField;
dataGrid.DataBind();
}
示例15: DataGridWebControlSupport
public void DataGridWebControlSupport()
{
var command = new NpgsqlCommand("select * from data;", Conn);
var dr = command.ExecuteReader();
//Console.WriteLine(dr.FieldCount);
var dg = new DataGrid();
dg.DataSource = dr;
dg.DataBind();
}