Cấp bậc tác giả:

DOTNET

Bằng cách nào để thêm cột và hàng trong DataTable để hiển thị trên Gridview

Được viết bởi webmaster ngày 01/11/2014 lúc 07:50 PM
Hướng dẫn các cách để thêm cột và hàng tự động
  • 0
  • 21058

Bằng cách nào để thêm cột và hàng trong DataTable để hiển thị trên Gridview

Thêm nhiều hàng vào DataTable
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



public partial class Example : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CretaeDatatablewithrows();
}

}
public void CretaeDatatablewithrows()
{
//Creating new instance of DataTable
DataTable dt = new DataTable();
//Adding columns to datatable
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Emailid", typeof(string));

//Adding Dummy data to datatable
for (int i = 1; i <= 10; i++)
{
DataRow row;
row = dt.NewRow();
row["Emailid"] = "dummy" + i + "@gmail.com";
row["ID"] = i;
dt.Rows.Add(row);
}
//Bind DataTable to GridView
GridView1.DataSource = dt;
GridView1.DataBind();

}
}
Kết quả hiển thị sau:

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML