anjali khan

anjali khan

  • NA
  • 293
  • 66.6k

gridview

Jul 24 2015 2:09 AM
i have a gridview in this i am displaying the data using without database..
in this i can do insert and update...
now i want if i click the perticular cell so i want to update without using the update button...
 
code
 
design page
 

<div>

Qualitifaction :<asp:TextBox ID="txtdata" runat="server"></asp:TextBox>&nbsp;&nbsp;&nbsp;&nbsp;

Year :<asp:TextBox ID="txtyear" runat="server"></asp:TextBox>

<br />

<br />

<asp:GridView ID="gvdynamic" runat="server" OnRowEditing="gvdynamic_RowEditing" OnRowCancelingEdit="gvdynamic_RowCancelingEdit"

OnRowUpdating="gvdynamic_RowUpdating" Width="439px">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:Button ID="btnedit" CommandName="EDIT" runat="server" Text="EDIT" />

</ItemTemplate>

<EditItemTemplate>

<asp:Button ID="btnupdate" CommandName="Update" runat="server" Text="Update" />

<asp:Button ID="btnCancel" CommandName="Cancel" runat="server" Text="Cancel" />

</EditItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

&nbsp;<asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" />

</div>

 
cs page
 

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

{

DataTable dt;

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

dt = new DataTable();

MakeTable();

}

else

{

dt = (DataTable)ViewState["dt"];

}

ViewState["dt"] = dt;

}

protected void MakeTable()

{

dt.Columns.Add("ID").AutoIncrement = true;

dt.Columns.Add("Qalification");

dt.Columns.Add("Year");

}

protected void AddRows()

{

DataRow dr = dt.NewRow();

dr["Qalification"] = txtdata.Text; //Qualification textbox

dr["Year"] = txtyear.Text;//Year textbox

dt.Rows.Add(dr);

}

protected void btnadd_Click(object sender, EventArgs e)

{

AddRows();

gvdynamic.DataSource = dt;

gvdynamic.DataBind();

}

protected void gvdynamic_RowEditing(object sender, GridViewEditEventArgs e)

{

gvdynamic.EditIndex = e.NewEditIndex;

gvdynamic.DataSource = dt;

gvdynamic.DataBind();

}

protected void gvdynamic_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

gvdynamic.EditIndex = -1;

gvdynamic.DataSource = dt;

gvdynamic.DataBind();

}

protected void gvdynamic_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

GridViewRow row = (GridViewRow)gvdynamic.Rows[e.RowIndex];

int Id = Convert.ToInt32(((TextBox)row.Cells[1].Controls[0]).Text);

string Qualification = ((TextBox)row.Cells[2].Controls[0]).Text;

string year = ((TextBox)row.Cells[3].Controls[0]).Text;

DataRow[] datarow = dt.Select("ID='" + Id + "'");

dt.Rows[Id].BeginEdit();

dt.Rows[Id]["Qalification"] = Qualification;

dt.Rows[Id]["Year"] = year;

dt.Rows[Id].EndEdit();

dt.AcceptChanges();

gvdynamic.EditIndex = -1;

gvdynamic.DataSource = dt;

gvdynamic.DataBind();

}

}