Skip to main content

How to edit GridView manually

Filling, Editing, Updating, Deleting from a GridView is quite easy now.
Just follow these steps.
1. Open Visual Studio create a web based project.
2. Go to design view of Default.aspx page and then drag&drop a GridView control from standard Visual Studio Toolbox.
3.Right click on GridView goto properties and then disable AutoGeneratedColumns property to false.
4. Navigate to your source page (default.aspx).
then we will found the tags look like

asp:gridview id="GridView1" runat="server" autogeneratecolumns="False">

5. Now, open tab in gridview and wirte the following code.


6. Save the File. Now goto your codebehind file(Default.aspx.cs)
7. Replace your Page_Load event with the following

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgrid();

}
}
8. And write the Grid method

private void fillgrid()
{
con.Open();
da = new SqlDataAdapter("select * from login", con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}

9. For GridView Editing Right click on GridView goto events and click in GridViewRowEditing event, it will generates a GridView1_RowEditing method in your codebehind file.
10. Now, in codebehind file write the following code

int i = Convert.ToInt32(e.NewEditIndex);

LinkButton lb1 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton1");
LinkButton lb2 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton2");
LinkButton lb3 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton3");
lb2.Visible = lb3.Visible = true;
lb1.Visible = false;
Label l1 = (Label)GridView1.Rows[i].FindControl("Label1");
Label l2 = (Label)GridView1.Rows[i].FindControl("label2");
l1.Visible = l2.Visible = false;

TextBox t1 = (TextBox)GridView1.Rows[i].FindControl("TextBox1");
TextBox t2 = (TextBox)GridView1.Rows[i].FindControl("Textbox2");
t1.Visible = t2.Visible = true;

11. For GridViewUpdating right click on gridview goto properties and then events double click on
GridViewUpdating event it'll generates a GridView1_RowUpdating event in your codebehind file.
12. Write the following code in that method

int i = e.RowIndex;
TextBox t1 = (TextBox)GridView1.Rows[i].FindControl("TextBox1");
TextBox t2 = (TextBox)GridView1.Rows[i].FindControl("Textbox2");
con.Open();
SqlCommand cmd = new SqlCommand("update login set name='" + t1.Text + "' where psw='" + t2.Text + "'",con );
t1.Visible = t2.Visible = false;

cmd.ExecuteNonQuery();
con.Close();


Label l1 = (Label)GridView1.Rows[i].FindControl("Label1");
Label l2 = (Label)GridView1.Rows[i].FindControl("label2");
l1.Visible = l2.Visible = true;

refreshgrid();

That's it !! It is that much easy to EDITING GRIDVIEW MANUALLY.
Lets' try to start your own logic to modify GridView

Comments

Popular posts from this blog

Splash Screen in Visual Studio 2008 using C#.NET

Creating Splash Screen is now very easy just follow these steps to make your application look very rich. 1.Suppose, Welcome.cs is your main form and SplashScreen.cs is your splash screen form. 2.Write the following code in the page_load event of your Welcome.cs form Hide(); bool done = false; ThreadPool.QueueUserWorkItem((x) => { using (var splashForm = new SplashForm()) { splashForm.Show(); while (!done) Application.DoEvents(); splashForm.Close(); } }); Thread.Sleep(3000); // Emulate hardwork done = true; Show(); 3. It's done!!

Difference between two Dates in C#.NET ?

To find the difference between two dates is very simple in VB — By using DateDiff method. But in C#, there is no direct method to do so. but there is a way to achive this. For this we need to understand TimeSpan Class. The following code snippet will show you how to find the difference. DateTime startTime = DateTime.Now; DateTime endTime = DateTime.Now.AddSeconds( 75 ); TimeSpan span = endTime.Subtract ( startTime ); Console.WriteLine( “Time Difference (seconds): ” + span.Seconds ); Console.WriteLine( “Time Difference (minutes): ” + span.Minutes ); Console.WriteLine( “Time Difference (hours): ” + span.Hours ); Console.WriteLine( “Time Difference (days): ” + span.Days ); By concatenating all this you will get the difference between two dates. You can also use span.Duration(). There are certain limitations to. The TimeSpan is capable of returning difference interms of Days, Hours, mins and seconds only. It is not having a property to show difference interms of Months and years. Hope this

How to store image in a database in binary format

Create one empty web page and do the following things... 1.Drag & Drop FileUpload Control and one Button from standard Toolbox 2.Double click on button control it'll generate a button click event. In that event write the following code 3.business_logic is a class file and which is used for opening database conections, executing database queries,etc. You can may even directly declare directly the connections, commands in codebehind file(*.cs,*.vb) business_logic obj = new business_logic(); if (FileUpload1.HasFile) { // Storing image in Database BinaryReader reader = new BinaryReader(FileUpload1.PostedFile.InputStream); byte[] image = reader.ReadBytes(FileUpload1.PostedFile.ContentLength); string temp= obj.assignconn(); con = new SqlConnection(temp); con.Open(); cmd.Connection = con; cmd.CommandText = "sp_imageInsert"; cmd.CommandType = CommandType.StoredPr