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..