本文整理汇总了C#中GridView.DataBind方法的典型用法代码示例。如果您正苦于以下问题:C# GridView.DataBind方法的具体用法?C# GridView.DataBind怎么用?C# GridView.DataBind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GridView
的用法示例。
在下文中一共展示了GridView.DataBind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GridViewDataBind
/// <summary>
/// 绑定数据到GridView,当表格数据为空时显示表头
/// </summary>
/// <param name="gridview"></param>
/// <param name="table">not null</param>
public static void GridViewDataBind(GridView gridview, DataTable table)
{
if (table == null)
throw new NullReferenceException();
//记录为空重新构造Gridview
if (table.Rows.Count == 0)
{
table = table.Clone();
table.Rows.Add(table.NewRow());
gridview.DataSource = table;
gridview.DataBind();
int columnCount = table.Columns.Count;
gridview.Rows[0].Cells.Clear();
gridview.Rows[0].Cells.Add(new TableCell());
gridview.Rows[0].Cells[0].ColumnSpan = columnCount;
gridview.Rows[0].Cells[0].Text = EmptyText;
gridview.Rows[0].Cells[0].Style.Add("text-align", "center");
}
else
{
//数据不为空直接绑定
gridview.DataSource = table;
gridview.DataBind();
}
//重新绑定取消选择
gridview.SelectedIndex = -1;
}
示例2: GridViewDataBind
///<summary>
///绑定数据到GridView,当表格数据为空时显示表头
///</summary>
///<param name="gridview"></param>
///<param name="table"></param>
public static void GridViewDataBind(GridView gridview, DataTable table)
{
//记录为空重新构造Gridview
EmptyText = Resources.MyGlobal.NoDataText;
if (table.Rows.Count == 0)
{
table = table.Clone();
table.Rows.Add(table.NewRow());
int columnCount = table.Columns.Count;
gridview.DataSource = table;
gridview.DataBind();
gridview.Rows[0].Cells.Clear();
gridview.Rows[0].Cells.Add(new TableCell());
gridview.Rows[0].Cells[0].ColumnSpan = columnCount;
gridview.Rows[0].Cells[0].Text = EmptyText;
gridview.Rows[0].Cells[0].Style.Add("text-align", "center");
gridview.Rows[0].Cells[0].Style.Add("color", "red");
gridview.Rows[0].Style.Add("height", "24px");
//gridview.HeaderRow.Enabled = false;
}
else
{
//数据不为空直接绑定
gridview.DataSource = table;
gridview.DataBind();
}
//重新绑定取消选择
gridview.SelectedIndex = -1;
}
示例3: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
YahooSession yahooSession = YahooAuthenticationProvider.Instance.LoadSession();
if (yahooSession.IsAuthenticated)
{
try
{
DataSet dsServices;
dsServices = yahooSession.GetService(new Uri(YahooServiceUrl));
foreach (DataTable dt in dsServices.Tables)
{
Label label = new Label();
label.Text = dt.TableName;
GridView gv = new GridView();
gv.ID = dt.TableName + "GridView";
gv.DataSource = dt;
gv.DataBind();
Panel1.Controls.Add(label);
Panel1.Controls.Add(gv);
}
}
catch (Exception ex)
{
Logger.Error("Failure with photos url: " + YahooServiceUrl, ex);
}
}
}
示例4: BindNullBomTable
static public void BindNullBomTable(int defaultRow,GridView gd)
{
DataTable dt = new DataTable();
DataRow dr = null;
//Part Type Description Part No/Item Name Qty PQty Collection Data PreCheck
dt.Columns.Add("Part Type");
dt.Columns.Add("Description");
dt.Columns.Add("Part No/Item Name");
dt.Columns.Add("Qty");
dt.Columns.Add("PQty") ;
dt.Columns.Add("Collection Data");
dt.Columns.Add("PreCheck");
for (int i = 0; i < defaultRow; i++)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
gd.DataSource = dt;
gd.DataBind();
gd.HeaderRow.Cells[3].Width = Unit.Percentage(5);
gd.HeaderRow.Cells[4].Width = Unit.Percentage(5);
}
示例5: GanDuLieu
public void GanDuLieu(GridView tmpGrid)
{
tblTaskList = TaskOBJ.DBConnectionTaskList("SP_TASK_VIEW", Convert.ToInt32(Session["logedUser"]), Convert.ToInt32(Session["TaskType"]));
if (tblTaskList.Rows.Count > 0)
{
dgrTaskList.Caption = "Danh sách công việc";//tblTaskList.Rows[0]["Danh sách hình ảnh đăng tải"].ToString();
tblTaskList.Columns.Add("Số TT", typeof(Int32));
// Add thứ tự công việc vào lưới
for (int i = 0; i < tblTaskList.Rows.Count; i++)
{
tblTaskList.Rows[i]["Số TT"] = i + 1;
}
tmpGrid.DataSource = tblTaskList;
tmpGrid.DataBind();
}
else
{
}
}
示例6: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
List<Event> events = Event.GetAllEvents();
foreach (Event ev in events)
{
Panel pnlEvent = new Panel();
pnlEvent.ID = ev.ID.ToString();
Label lblEventName = new Label();
lblEventName.Text = ev.Name;
Label lblNumberOfParticipants = new Label();
pnlEvent.Controls.Add(lblEventName);
pnlEvent.Controls.Add(lblNumberOfParticipants);
GridView grdvwParticipation = new GridView();
grdvwParticipation.DataSource = Event.GetRegisteredParticipants(ev, 0);
grdvwParticipation.DataBind();
pnlEvent.Controls.Add(grdvwParticipation);
pnlDetails.Controls.Add(pnlEvent);
if (ev.IsSinglePlayer)
{
foreach (GridViewRow row in grdvwParticipation.Rows)
{
row.Cells[0].Text = "TT" + row.Cells[0].Text;
}
lblNumberOfParticipants.Text = "Total Participants: " + grdvwParticipation.Rows.Count.ToString();
}
else
{
var numberOfTeams = (from i in grdvwParticipation.Rows.Cast<GridViewRow>() select new { Teamid = i.Cells[0].Text }).Distinct().Count();
lblNumberOfParticipants.Text = "Total Teams: " + numberOfTeams.ToString();
}
}
}
示例7: ExportGridToExcel
private void ExportGridToExcel(GridView grid, string filename)
{
if (string.IsNullOrEmpty(filename))
throw new ArgumentException(
"Export filename is required");
if (!filename.EndsWith(".xls"))
filename += ".xls";
grid.AllowPaging = false;
grid.AllowSorting = false;
grid.DataBind();
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader(
"content-disposition",
"attachment;filename=" + filename);
Response.Charset = string.Empty;
Page.EnableViewState = false;
grid.RenderControl(hw);
Response.Write(
"<html xmlns:x=\"urn:schemas-microsoft-com:" +
"office:excel\" >");
Response.Write(GetExcelStyle(grid));
Response.Write(tw.ToString());
Response.End();
}
示例8: OutputExcel
/// <summary>
/// 輸出為Excel
/// </summary>
/// <param name="webpage">傳入目前網頁Page變數</param>
/// <param name="Dt">需要匯出的內容</param>
/// <param name="FilePath">輸出的檔名</param>
public void OutputExcel(Page webpage,
DataTable Dt,
string FilePath
)
{
GridView gvExport = new GridView();
gvExport.DataSource = Dt;
gvExport.DataBind();
string UTF8FileName = HttpUtility.UrlEncode(FilePath,
System.Text.Encoding.UTF8
);
webpage.Response.Clear();
webpage.Response.AddHeader("content-disposition",
string.Format("attachment;filename={0}",
UTF8FileName)
);
webpage.Response.Cache.SetCacheability(HttpCacheability.NoCache);
webpage.Response.ContentType = "application/vnd.ms-excel";
webpage.Response.Charset = "big5";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvExport.RenderControl(htmlWrite);
webpage.Response.Write(stringWrite.ToString().Replace("<div>", "").Replace("</div>", ""));
webpage.Response.End();
}
示例9: btn1_Click
protected void btn1_Click(object sender, EventArgs e)
{
ControlClass d = new ControlClass(txtAspContent.Text);
List<p1> lst = new List<p1>();
lst.Add(new p1 { Name = "hi1" });
lst.Add(new p1 { Name = "hi2" });
lst.Add(new p1 { Name = "hi3" });
lst.Add(new p1 { Name = "hi4" });
GridView ss = new GridView();
ss = ((GridView)d.ControlGenerated);
ss.DataSource = lst;
ss.DataBind();
foreach (GridViewRow row in ss.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label myHyperLink = row.FindControl("lbl1") as Label;
Label myHyperLnk1 = row.FindControl("Name2") as Label;
}
}
foreach (GridViewRow row in gg1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label myHyperLink = row.FindControl("lbl1") as Label;
}
}
plcHolder1.Controls.Add(ss);
}
示例10: FillRunningOrdersGrid
public void FillRunningOrdersGrid(GridView gv, bool display)
{
if (!string.IsNullOrEmpty(Show_ID))
{
Guid? show_Final_Class_ID = null;
if (!string.IsNullOrEmpty(Show_Final_Class_ID) && Show_Final_Class_ID != new Guid().ToString())
{
show_Final_Class_ID = new Guid(Show_Final_Class_ID);
}
Guid show_ID = new Guid(Show_ID);
List<Guid> showList = new List<Guid>();
showList.Add(show_ID);
LinkedShows ls = new LinkedShows();
List<LinkedShows> lsList = ls.GetLinked_ShowsByParent_ID(show_ID);
if (lsList != null && lsList.Count > 0)
{
foreach (LinkedShows linkedShow in lsList)
{
showList.Add(linkedShow.Child_Show_ID);
}
}
RunningOrders.SetDay1Show_ID(showList);
List<OwnersDogsClassesDrawn> oDCDList = OwnersDogsClassesDrawn.GetOwnersDogsClassesDrawnListData(show_ID, show_Final_Class_ID, display);
if (display)
{
gv.DataSource = oDCDList;
gv.DataBind();
}
else
{
OwnersDogsClassesDrawnList = oDCDList;
}
}
}
示例11: ExportExcel
public void ExportExcel(DataTable dt, string title, string from_time, string to_time, string User)
{
GridView GridView8 = new GridView();
GridView8.AllowPaging = false;
DataTable dt_Header = new DataTable();
dt_Header.Columns.Add("Thong_Tin", typeof(string));
dt_Header.Rows.Add("");
dt_Header.Rows.Add("" + title);
dt_Header.Rows.Add("");
dt_Header.Rows.Add("User: " + User);
dt_Header.Rows.Add("");
dt_Header.Rows.Add("Tu ngay: " + from_time);
dt_Header.Rows.Add("Den ngay: " + to_time);
dt_Header.Rows.Add("");
GridView GridView7 = new GridView();
GridView7.AllowPaging = false;
GridView7.DataSource = dt_Header;
GridView7.BorderStyle = BorderStyle.None;
GridView7.DataBind();
//Add row if no data found
//if (dt.Rows.Count <= 0)
//{
// int num_col = dt.Columns.Count;
// dt.Rows.Add();
//}
GridView8.DataSource = dt;
GridView8.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + User + "_Report.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 0; i < GridView7.Rows.Count; i++)
{
//Apply text style to each Row
GridView7.Rows[i].Attributes.Add("class", "textmode");
}
GridView7.RenderControl(hw);
for (int i = 0; i < GridView8.Rows.Count; i++)
{
//Apply text style to each Row
GridView8.Rows[i].Attributes.Add("class", "textmode");
}
GridView8.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
示例12: BindDG
public static void BindDG(GridView dg, string id, string strSql, string Tname)
{
SqlConnection conn = DBCon();
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds, Tname);
dg.DataSource = ds.Tables[Tname];
dg.DataKeyNames = new string[] { id };
dg.DataBind();
}
示例13: bindData
//Bind data to a gridview
internal void bindData(string column, string condition, string table, GridView GridView1)
{
string sql = @"SELECT " + column + " from " + table + condition + ";";
SqlDataAdapter da = new SqlDataAdapter(sql, cnn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
DataTable dt = ds.Tables["data"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
示例14: bindDataSource
/// <summary>
/// Use for displaying out a data table by gridview
/// </summary>
/// <param name="gridview"></param>
/// <param name="str_caption">caption of gridview</param>
/// <param name="ds">DataSet</param>
protected void bindDataSource(GridView gridview, string str_caption, DataSet ds)
{
bool flag = true;
if (ds != null)
{
if (ds.Tables[0].Rows.Count == 0)
{
flag = false;
sql.getNullDataSet(ds);
}
//By Lhy 20110512 ITEM18 DEL Start
//gridview.Width = Unit.Pixel(260);
//By Lhy 20110512 ITEM18 DEL End
gridview.AutoGenerateColumns = false;
gridview.AllowPaging = false;
gridview.Visible = true;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
BoundField bf = new BoundField();
bf.DataField = ds.Tables[0].Columns[i].ColumnName.ToString();
bf.HeaderText = ds.Tables[0].Columns[i].Caption.ToString();
bf.ItemStyle.Width = 200;
bf.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
bf.HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
gridview.Columns.Add(bf);
}
CommandField cf_Delete = new CommandField();
cf_Delete.ButtonType = ButtonType.Image;
cf_Delete.ShowDeleteButton = true;
cf_Delete.ShowCancelButton = true;
cf_Delete.CausesValidation = false;
cf_Delete.DeleteImageUrl = "~/images/del.jpg";
cf_Delete.DeleteText = "Delete";
gridview.Columns.Add(cf_Delete);
gridview.AllowSorting = true;
//By Lhy 20110512 ITEM18 DEL End
//gridview.Caption = str_caption;
//gridview.CaptionAlign = TableCaptionAlign.Top;
//By Lhy 20110512 ITEM18 DEL End
gridview.DataSource = ds.Tables[0];
gridview.DataBind();
gridview.Columns[0].Visible = false;
gridview.Columns[gridview.Columns.Count - 1].Visible = flag;
if (getRoleID(getRole()) != "0")
gridview.Columns[gridview.Columns.Count - 1].Visible = false;
}
}
示例15: Pager
protected void Pager(GridView dl,Wuqi.Webdiyer.AspNetPager anp,System.Data.DataSet dst)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = dst.Tables[0].DefaultView;
pds.AllowPaging = true;
anp.RecordCount = dst.Tables[0].DefaultView.Count;
pds.CurrentPageIndex = anp.CurrentPageIndex - 1;
pds.PageSize = anp.PageSize;
dl.DataSource = pds;
dl.DataBind();
}