473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Gridview Data not binded on second time execution.

4
Hi,

I have two gridviews to which data is binded when page is not posted back.

when i run for first time the data is binded to gridview, and when i run next time the data is not binded to grid view.

the code is:


public partial class _Default : System.Web.UI.Page

{

public static DataTable tableGrid1 = new DataTable();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindData();

}

}



public void BindData()

{



DataColumn columnGrid1 = new DataColumn(); // Binding data to the tableGrid1

columnGrid1.ColumnName = "TeamName";

tableGrid1.Columns.Add(columnGrid1);



DataRow dr1 = tableGrid1.NewRow();

dr1[0] = "AAAAAAA";

tableGrid1.Rows.Add(dr1);

DataRow dr2 = tableGrid1.NewRow();

dr2[0] = "BBBBBBBB";

tableGrid1.Rows.Add(dr2);

DataRow dr3 = tableGrid1.NewRow();

dr3[0] = "CCCCCCC";

tableGrid1.Rows.Add(dr3);

DataRow dr4 = tableGrid1.NewRow();

dr4[0] = "DDDDDDDDD";

tableGrid1.Rows.Add(dr4);

DataRow dr5 = tableGrid1.NewRow();

dr5[0] = "EEEEEEEEE";

tableGrid1.Rows.Add(dr5);



tableGrid1.Rows.Add(tableGrid1.NewRow());



GridView1.DataSource = tableGrid1;

GridView1.DataBind();

}

}



when i run second time without rebuild it is not working

pls help me...

i m going mad !!!

bye
hydson
Feb 20 '08 #1
6 2062
int08h
28
set a breakpoint at line "if (!IsPostBack)" and observe whether IsPostBack is true or false, if it is true the second time you refresh the page, it is majorly a problem with your web browser
Feb 20 '08 #2
dip_developer
648 Expert 512MB
Hi,

I have two gridviews to which data is binded when page is not posted back.

when i run for first time the data is binded to gridview, and when i run next time the data is not binded to grid view.

the code is:


public partial class _Default : System.Web.UI.Page

{

public static DataTable tableGrid1 = new DataTable();

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

BindData();

}

}



public void BindData()

{



DataColumn columnGrid1 = new DataColumn(); // Binding data to the tableGrid1

columnGrid1.ColumnName = "TeamName";

tableGrid1.Columns.Add(columnGrid1);



DataRow dr1 = tableGrid1.NewRow();

dr1[0] = "AAAAAAA";

tableGrid1.Rows.Add(dr1);

DataRow dr2 = tableGrid1.NewRow();

dr2[0] = "BBBBBBBB";

tableGrid1.Rows.Add(dr2);

DataRow dr3 = tableGrid1.NewRow();

dr3[0] = "CCCCCCC";

tableGrid1.Rows.Add(dr3);

DataRow dr4 = tableGrid1.NewRow();

dr4[0] = "DDDDDDDDD";

tableGrid1.Rows.Add(dr4);

DataRow dr5 = tableGrid1.NewRow();

dr5[0] = "EEEEEEEEE";

tableGrid1.Rows.Add(dr5);



tableGrid1.Rows.Add(tableGrid1.NewRow());



GridView1.DataSource = tableGrid1;

GridView1.DataBind();

}

}



when i run second time without rebuild it is not working

pls help me...

i m going mad !!!

bye
hydson
check whether Gridview's EnableViewState property is True or False............if False ...make it to True
Feb 20 '08 #3
hydson
4
set a breakpoint at line "if (!IsPostBack)" and observe whether IsPostBack is true or false, if it is true the second time you refresh the page, it is majorly a problem with your web browser


hi thanks

i checked second time and its true..

wat can be the problem wid the browser ???

thanks

hydson :-)
Feb 20 '08 #4
hydson
4
check whether Gridview's EnableViewState property is True or False............if False ...make it to True

hi thanks a lot

enableview state is true only

pls advice further...

bye

hydson.
Feb 20 '08 #5
kunal pawar
297 100+
Use ur bind function out of !Ispostback
or

define ur datatable object tablegrid in bind function
Feb 20 '08 #6
hydson
4
Use ur bind function out of !Ispostback
or

define ur datatable object tablegrid in bind function




hi i want to give full description of wat i am doing.

i have 2 grdiviews .

one is assigned another unassigned

i have some teams binded to grid1 . now on click n dbl click i need to bind them to grid2.

on running all the teams are binded to grid1. and click n dbl click they are binded to grid2

now if i close the window. and do start without debugging i m not finding any data in grid1.



pls go thru dis code

public partial class _Default : System.Web.UI.Page, IPostBackEventHandler

{

// Declaration of tables and static variables

public static DataTable tableGrid1 = new DataTable();public static DataTable tableGrid2 = new DataTable();


DataColumn columnGrid2 = new DataColumn();

static int j1 = 0;public static DataSet ds = new DataSet();


protected void Page_Load(object sender, EventArgs e)
{



if (j1 == 0)
{

columnGrid2.ColumnName = "TeamName";
tableGrid2.Columns.Add(columnGrid2);

tableGrid2.Rows.Add(tableGrid2.NewRow());

GridView2.DataSource = tableGrid2;

GridView2.DataBind();

j1++;

DataColumn columnGrid1 = new DataColumn(); // Binding data to the tableGrid1

columnGrid1.ColumnName = "TeamName";
tableGrid1.Columns.Add(columnGrid1);

DataRow dr1 = tableGrid1.NewRow();dr1[0] = "Sales";
tableGrid1.Rows.Add(dr1);

DataRow dr2 = tableGrid1.NewRow();

dr2[0] = "Deployment";
tableGrid1.Rows.Add(dr2);

DataRow dr3 = tableGrid1.NewRow();dr3[0] = "Maintenance";
tableGrid1.Rows.Add(dr3);

DataRow dr4 = tableGrid1.NewRow();

dr4[0] = "Marketing";
tableGrid1.Rows.Add(dr4);

DataRow dr5 = tableGrid1.NewRow();dr5[0] = "Production";
tableGrid1.Rows.Add(dr5);

tableGrid1.Rows.Add(tableGrid1.NewRow());

ds.Tables.Add(tableGrid1);

GridView1.DataSource = ds;

GridView1.DataBind();

}



}

protected DataTable deleteRowGrid1(string index, DataTable sourceTable) //function to delete a row in grid1

{

for (int i = 0; i < sourceTable.Rows.Count; i++)
{

if (i == int.Parse(index))
{

sourceTable.Rows.RemoveAt(i);

}



}

tableGrid1 = sourceTable;

return sourceTable;
}

protected DataTable addNewRowGrid2() // function to add a row to grid2

{

DataRow newRow = tableGrid2.NewRow();string hdnRowData = hdn2.Value.ToString();
newRow[0] = hdnRowData;

tableGrid2.Rows.Add(newRow);

return tableGrid2;
}

protected void Button1_Click(object sender, EventArgs e) // ButtonClick event which deletes selected row from grid1 and adds to Grid2

{

string hdnRowIndex = hdn1.Value.ToString();


foreach (GridViewRow row in GridView1.Rows)
{

if (row.RowIndex == int.Parse(hdnRowIndex))
{

GridView1.DataSource = deleteRowGrid1(hdnRowIndex, tableGrid1);

GridView1.DataBind();



GridView2.DataSource = addNewRowGrid2(); // To add the selected row into assigned list

GridView2.DataBind();

}

}



}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Attributes.Add("onclick", "SelectRowGrid1('" + e.Row.Cells[0].FindControl("lbl").ClientID + "'," + e.Row.DataItemIndex.ToString() + ")");e.Row.Attributes.Add("ondblclick", Page.ClientScript.GetPostBackEventReference(this, "1"));
}

}



public void RaisePostBackEvent(string eventArgument)
{

if (!string.IsNullOrEmpty(eventArgument))
{

if (eventArgument == "1")
{

selectGrid1();

}

if (eventArgument == "2")
{

selectGrid2();

}

}

}

public void selectGrid1() // function to select a row on double click and delete from Grid1 and add to Grid2

{

string hdnRowIndex = hdn1.Value.ToString();foreach (GridViewRow row in GridView1.Rows)
{

if (row.RowIndex == int.Parse(hdnRowIndex))
{

GridView1.DataSource = deleteRowGrid1(hdnRowIndex, tableGrid1);

GridView1.DataBind();



GridView2.DataSource = addNewRowGrid2(); // To add the selected row to Grid2

GridView2.DataBind();

}

}



}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

e.Row.Attributes.Add("onclick", "SelectRowGrid2('" + e.Row.Cells[0].FindControl("lbl2").ClientID + "'," + e.Row.DataItemIndex.ToString() + ")");e.Row.Attributes.Add("ondblclick", Page.ClientScript.GetPostBackEventReference(this, "2"));
}

}



public void selectGrid2() // function to select a row on double click and delete from Grid2 and add to Grid1

{

string hdnRowIndex = hdn4.Value.ToString();tableGrid2.Rows.RemoveAt(int .Parse(hdnRowIndex));


GridView2.DataSource = tableGrid2;

GridView2.DataBind();

GridView1.DataSource = addNewRowGrid1();

GridView1.DataBind();

}

protected void Button3_Click(object sender, EventArgs e) // ButtonClik event which delete selected row from Grid2 and add to Grid1

{

string hdnRowIndex = hdn4.Value.ToString();tableGrid2.Rows.RemoveAt(int .Parse(hdnRowIndex));


GridView2.DataSource = tableGrid2;

GridView2.DataBind();

GridView1.DataSource = addNewRowGrid1();

GridView1.DataBind();

}

protected DataTable addNewRowGrid1() // Function to add a row to Grid1

{

DataRow newRow =tableGrid1.NewRow();string hdnRowData = hdn3.Value.ToString();
newRow[0] = hdnRowData;

tableGrid1.Rows.Add(newRow);



return tableGrid1;
}

protected void Button2_Click(object sender, EventArgs e) // ButtonClick event to delete all rows from Grid1 and add to Grid2

{

int r = tableGrid1.Rows.Count;while (r > 0)
{

DataRow newrow = tableGrid2.NewRow();
newrow[0] = tableGrid1.Rows[0].ItemArray.GetValue(0).ToString();

if (tableGrid1.Rows[0].ItemArray.GetValue(0).ToString() != "")
{

tableGrid2.Rows.Add(newrow);

}

tableGrid1.Rows[0].Delete();

r--;

}

if (tableGrid1.Rows.Count == 0)
{

tableGrid1.Rows.Add(tableGrid1.NewRow());

}

GridView1.DataSource =tableGrid1;

GridView1.DataBind();

GridView2.DataSource = tableGrid2;

GridView2.DataBind();

}

protected void Button4_Click(object sender, EventArgs e) // ButtonClick event to delete all rows from Grid2 and add to Grid1

{

int r = tableGrid2.Rows.Count;while (r > 0)
{

DataRow newrow =tableGrid1.NewRow();
newrow[0] = tableGrid2.Rows[0].ItemArray.GetValue(0).ToString();

if (tableGrid2.Rows[0].ItemArray.GetValue(0).ToString() != "")
{

tableGrid1.Rows.Add(newrow);

}

tableGrid2.Rows[0].Delete();

r--;

}

if (tableGrid2.Rows.Count == 0)
{

tableGrid2.Rows.Add(tableGrid2.NewRow());

}

GridView2.DataSource = tableGrid2;

GridView2.DataBind();

GridView1.DataSource = tableGrid1;

GridView1.DataBind();

}

}

when i close n do start without debugging den no data is present in grid1.



enable view state for grid1 is true only..
Feb 21 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: washoetech | last post by:
I have a gridview control. In this grid view there is a column for the price of an item. Some of the prices have a dollar sign in front of it and some dont. How do I get rid of the dollar sign...
6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
1
by: JN | last post by:
Hi All, I have a tree view which has parent nodes of each letter of the alphabet. The root node will be the customers name that relate to the parent node letter. I also have a gridview which is...
5
by: srinivas | last post by:
Hi, I have binded a dataset to a gridview control at run time. I'm fetching 5 columns from the database. But while displaying it on the screen i want to show only 3 columns. I tried with...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
1
by: mitchman10 | last post by:
My Time table has TimeID,Employee,PayPeriod,ChargeCodeID,Hours My Chargecode table has ChargecodeID,c_Text I need an Editable datagrid that will show the TimeID,Employee,PayPeriod,C_Text in a...
12
by: Cindy Lee | last post by:
When I do a sorta on 1 table, then the other table goes back to the original order. What can I set so, it keeps the order of the other current gridview's order. I set all the gridview values...
2
by: mahesh123 | last post by:
Hi, I am using gridview in my form and binded the gridview with object datasource.I am trying to implement sorting for my grid view and i am getting following error.i am using asp.net with VB ...
0
by: =?Utf-8?B?ZWdzZGFy?= | last post by:
Hi, I have created a second gridview when customer clicks on select link with this code: Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.