Subscribe
TechTasks.net

The Blog is Moved to www.TechTasks.net

Non Editable Rows in GridView

Posted by Rajesh Kumar Chekuri on



In This example A GridView containes non editable rows,

Here We use “Bit” DataType for Editable colume in sql server

When we use “Bit” Datatype the gridview binds the colume with Check boxes automatically



Productid (int)

Productname (Varchar)

price (int)

editable (bit)


1

keyboard

500

True


2

Monitor

5000

False


3

mouse

400

False


4

HDD

3000

True




<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" DataKeyNames="productid,editable"

OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"

OnRowUpdating="GridView1_RowUpdating" Style="z-index: 100; left: 222px; position: absolute;

top: 75px">

asp:GridView>

<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Red" Style="z-index: 102;

left: 311px; position: absolute; top: 51px">asp:Label>


In .cs File




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

{

SqlConnection con; SqlDataAdapter da; SqlCommand cmd; DataSet ds;

protected void Page_Load(object sender, EventArgs e)

{

con = new SqlConnection("server=FOCALPOI-392A7D\\SQLEXPRESS;user id=raji;password=chekuri;database=raji");

if (IsPostBack == false)

{

getdata();

}

}

public void getdata()

{

da = new SqlDataAdapter("select * from product", con);

ds = new DataSet();

da.Fill(ds, "product");

GridView1.DataSource = ds;

GridView1.DataBind();

}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

Label1.Text = "";

GridViewRow r = GridView1.Rows[e.NewEditIndex];

CheckBox cb = (CheckBox)r.Cells[4].Controls[0];

//if the Check box in editable colume is checked then the colume is allow to edit

//if not then it is non Editable colume

if (cb.Checked)

{

GridView1.EditIndex = e.NewEditIndex;

getdata();

}

else

{

Label1.Text = "This Is Non Editable Item";

}

}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1;

getdata();

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

GridView1.EditIndex = -1;

getdata();

}

}

Bookmark and Share
Non Editable Rows in GridViewSocialTwist Tell-a-Friend

2 comments:

final year projects said...

Thanx a lot for the great, informative article, saved me by providing this post, keep doing many.

latest microcontroller said...

Great tutorial, nicely explained, most useful post, keep doing.

Subscribe to: Post Comments (Atom)
Thanks For Visiting