hi.,:cool:

I need to create a grid view in asp.net which should have the database connectivity nor sqldatasource. hence i should use the dataset,datatable,datarow and some how i tried and create the dataset and displayed in grid view.

i wrote this code in page load event:

dataset ds = new dataset();
datatable dt = new datatable();
datacolumn dc = dt.columns.add("Emp",typeof(string));
datarow dr;
dt.rows.add(dr);
dataview dv = new dataview(dt);
gridview1.datasource=dv;
gridview1.databind();


and i used a buttin and textbox to add info to that column

dr[0]=textbox1.text.tostrting();
dt.rows.add(Dr);
dr=dt.newrow();


this code add the values to column but it doesnot increment the row. the values are just pasted above that same row.

so could any one help me sooon..
i need some code or send me some link where i can learn easily about this.
need to implement this today(8-7-2010) before 2'o clock so make it fast guys :icon_question:

Recommended Answers

All 9 Replies

Hello nezammca,

If you are asking for populating GridView without hit in the DB. Then you can try this.

#Region "Page Methods"

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			PopulateGridView()
		End If
	End Sub

#End Region

#Region "Methods"
	Public Sub PopulateGridView()
		Dim dt As New DataTable()
		dt.Columns.Add("Name")
		dt.Columns.Add("TextData")
		dt.Columns.Add("OtherInfo")
		For index As Integer = 1 To 10 Step 1
			dt.Rows.Add("Name" & index, "This is DataText" & index, "OtherInfo" & 1)
		Next
		gvPopulate.DataSource = dt
		gvPopulate.DataBind()
	End Sub

#End Region

Hope it works ,
:icon_smile:

commented: good response! +1

yes it will work :)
and i am doing in something in different manner

sorry i am in need of c # code ?

Never mind, Here it is.

#region "Page Methods"

	protected void Page_Load(object sender, System.EventArgs e)
{
	if (!Page.IsPostBack) {
		PopulateGridView();
	}
}

#endregion

#region "Methods"
public void PopulateGridView()
{
	System.Data.DataTable dt = new System.Data.DataTable();
	dt.Columns.Add("Name");
	dt.Columns.Add("TextData");
	dt.Columns.Add("OtherInfo");
	for (int index = 1; index <= 10; index += 1) {
		dt.Rows.Add("Name" + index, "This is DataText" + index, "OtherInfo" + 1);
	}
	gvPopulate.DataSource = dt;
	gvPopulate.DataBind();
}

#endregion

Also remember to mark it as solved when you get your answer :)

DataSet ds = new DataSet();
DataTable dt = new DataTable();
int index = 1;
DataView dv;
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{

}
private void designcol()
{
    DataColumn dc = dt.Columns.Add("EMPLOYEE NO", typeof(string));

}

protected void Button1_Click(object sender, EventArgs e)
{
    designcol();

    dr = dt.NewRow();
    dr[0] = TextBox1.Text;
    dt.Rows.Add(dr);
    dv = new DataView(dt);
    GridView1.DataSource = dv;
    GridView1.DataBind();
}
}

hi frdz.,

what i have done is i have added a textbox and a button. when i press a button column is created and values from textbox are stored in gridview . now whats the problem is when ever i add the values it overwrites above it :( . i am facing this problem could some one help me out ?

Frdz Clear up This error Dudes !!!!

HI FRDZ.,

I GOT THIS CODE FROM A PERSON CALLED ANAND MALLI THANKS TO HIM :)


It is because you are creating new instance of your DataView everytime if you see,so what instead take your DataView in ViewState["myDataView"] and everytime first get the DataView from this ViewState and than add rows to it,add that DataView again to ViewState and than bind it to your grid,so it will preserve your rows,as you know when asp.net page gets post back you have use ViewState to preserve your Values like following

protected void Button1_Click(object sender, EventArgs e)
{
designcol();
if(ViewState["myDataTable"]!=null)
{
DataTable tmpDt=new DataTable();
tmpDt=ViewState["myDataTable"] as DataTable;
dt=tmpDt.Copy();
}
dr = dt.NewRow();
dr[0] = TextBox1.Text;
dt.Rows.Add(dr);
ViewState["myDataTable"]=dt; // Update the ViewState
dv = new DataView(dt);
GridView1.DataSource = dv;
GridView1.DataBind();
}


AND THIS WORKED WELL....

i want that I can Insert data in Grid without attecting Database. and In grid We Can see Both (From Database and Our Inserted Data)
wht should i do???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.