Sie sind auf Seite 1von 5

Hello Friends.......

Gridview is the most common data control in


asp.net 2.0.It is a very rich control indeed.Here we are going to learn how to use
gridview control for handling data.Here we will create a gridview control to
retrieve the data from the database and we will perform the operation like show,
update, cancel, paging, sorting, add and delete on the retrieved data.

So lets start the fun.First of all just create


a create a Gridview control on your aspx page(you can do this by simply draging
'n' dropping the gridview control on the page)
Now right click on the gridview control and choose 'Auto Format' from the context
menu.Now from the Auto format window you can simply choose the scheme acoording to
your interest.
(in our case, we have selected the 'state' scheme).After selecting the scheme ,
your control will appear like this in apspx page :

<font style="color: rgb(51, 51, 153);">< asp:GridView id="GridView1" width="70%"


runat="server" style="position: relative"> <font style="color: rgb(51, 51, 153);">
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
BorderWidth="1px"</font> <font style="color: rgb(51, 51, 153);">
DataKeyNames="Name" CellPadding="3" AllowPaging="True"
AutoGenerateColumns="false"</font> <font style="color: rgb(51, 51, 153);">
AllowSorting="True" GridLines="Horizontal" </font> <font style="color: rgb(51, 51,
153);"> PageSize="5" </font><font style="color:
rgb(51, 51, 153);">>

< FooterStyle backcolor="#B5C7DE" forecolor="#4A3C8C">


< RowStyle backcolor="#E7E7FF" forecolor="#4A3C8C">
< PagerStyle backcolor="#E7E7FF" forecolor="#4A3C8C"
horizontalalign="Right">
< SelectedRowStyle backcolor="#738A9C" bold="True"
forecolor="#F7F7F7">
< HeaderStyle backcolor="#4A3C8C" bold="True"
forecolor="#F7F7F7">
< AlternatingRowStyle backcolor="#F7F7F7">
< /asp:GridView>

</font></font>Now bind the columns of your database table to the gridview in the
following manner :<font style="color: rgb(51, 51, 153);"><font style="color:
rgb(51, 51, 153);">
</font></font> (Insert Before footer template tag.....)<font style="color: rgb(51,
51, 153);"><font style="color: rgb(51, 51, 153);">

< Columns>
< asp:TemplateField HeaderText="Name">
< ItemTemplate>
< %# Eval("Name") %>
< /ItemTemplate>
< EditItemTemplate>
< asp:TextBox ID="txtName"
runat="server" Text='<%# Bind("Name") %>' />
< /EditItemTemplate>
< ItemStyle Font-Bold="True" />
< /asp:TemplateField>
< asp:TemplateField HeaderText="Designation">
< ItemTemplate>
< %# Eval("Designation") %>
< /ItemTemplate>
< EditItemTemplate>
< asp:TextBox ID="txtDesig"
runat="server" Text='<%# Bind("Designation") %>' />
< /EditItemTemplate>
< ItemStyle Font-Bold="True" />
< /asp:TemplateField>
< asp:TemplateField HeaderText="Company">
< ItemTemplate>
< %# Eval("Company") %>
< /ItemTemplate>

< EditItemTemplate>
< asp:TextBox ID="txtComp"
runat="server" Text='<%# Bind("Company") %>' />
< /EditItemTemplate>
< ItemStyle Font-Bold="True" />
< /asp:TemplateField>
< asp:TemplateField HeaderText="Experience">
< ItemTemplate>
< %# Eval("Experience") %>
< /ItemTemplate>

< EditItemTemplate>
< asp:TextBox ID="txtExp"
runat="server" Text='<%# Bind("Experience") %>' />
< /EditItemTemplate>
< ItemStyle Font-Bold="True" />
< /asp:TemplateField>
< asp:CommandField ShowEditButton="True" />
< /Columns>

</font></font>And in design view, it will apeear like this :


<font style="color: rgb(51, 51, 153);">

<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"


href="http://1.bp.blogspot.com/_avkw6l8uMYs/Spkxs6FORII/AAAAAAAABIs/3REhLVYQe9Q/s1
600-h/untitled.JPG"><img style="margin: 0px auto 10px; display: block; text-align:
center; cursor: pointer; width: 320px; height: 192px;"
src="http://1.bp.blogspot.com/_avkw6l8uMYs/Spkxs6FORII/AAAAAAAABIs/3REhLVYQe9Q/s32
0/untitled.JPG" alt="" id="BLOGGER_PHOTO_ID_5375382277818565762"
border="0"></a></font>
Now moving towards the code behind(aspx.cs) part, we have to retrieve
the data from the database and bind it to the Gridview control.
here for retrieving and binding purpose, we create a simple BindGrid function as
follows :

<font style="color: rgb(51, 51, 153);">public void BindGrid()</font>

<font style="color: rgb(51, 51, 153);">{</font>

<font style="color: rgb(51, 51, 153);"> con.Open();</font>

<font style="color: rgb(51, 51, 153);"> DataSet ds = new DataSet();</font>

<font style="color: rgb(51, 51, 153);"> string str = "select Name, Designation,
Company, Experience from Profile";</font>
<font style="color: rgb(51, 51, 153);"> SqlDataAdapter da = new
SqlDataAdapter(str, con);</font>

<font style="color: rgb(51, 51, 153);"> da.Fill(ds);</font>

<font style="color: rgb(51, 51, 153);"> GridView1.DataSource = ds;</font>

<font style="color: rgb(51, 51, 153);"> GridView1.DataBind();</font>

<font style="color: rgb(51, 51, 153);"> con.Close();</font>

<font style="color: rgb(51, 51, 153);">}</font>

........and on the page load event, we will call the function in the
following manner :

<font style="color: rgb(51, 51, 153);">SqlConnection con = new SqlConnection("Data


Source=kartik;Initial Catalog=MyNetwork;Integrated Security=true");</font>

<font style="color: rgb(51, 51, 153);">protected void Page_Load(object sender,


EventArgs e)</font>

<font style="color: rgb(51, 51, 153);">{</font>

<font style="color: rgb(51, 51, 153);"> if(!Page.IsPostBack)</font>

<font style="color: rgb(51, 51, 153);"> BindGrid();</font>

<font style="color: rgb(51, 51, 153);">}</font>

We will also use the PageIndexChanging event of


gridview for implementing Page Indexing...............

<font style="color: rgb(51, 51, 153);">protected void


GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)</font>

<font style="color: rgb(51, 51, 153);">{</font>

<font style="color: rgb(51, 51, 153);"> GridView1.PageIndex =


e.NewPageIndex;</font>

<font style="color: rgb(51, 51, 153);"> BindGrid();</font>

<font style="color: rgb(51, 51, 153);">}</font>

.............Now we are ready to see how our


gridview look in web borowser.So just run your application and check your gridview
in the browser.The gridview will look like this at run time :

<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"


href="http://3.bp.blogspot.com/_avkw6l8uMYs/SpkzmkcdMqI/AAAAAAAABI0/W6JB2S0ZDx4/s1
600-h/untitled1.JPG"><img style="margin: 0px auto 10px; display: block; text-
align: center; cursor: pointer; width: 320px; height: 113px;"
src="http://3.bp.blogspot.com/_avkw6l8uMYs/SpkzmkcdMqI/AAAAAAAABI0/W6JB2S0ZDx4/s32
0/untitled1.JPG" alt="" id="BLOGGER_PHOTO_ID_5375384367954473634" border="0"></a>
................Now we add the Edit button to our gridview control.Simply right
click on the control, choose 'show smart tag' from the context menu, select 'Edit
Columns' and then select 'command field' from the field window....after cicking on
'command field' , you will find the '
edit,Update,cancel' button option, select it and add it.It will now be showing in
your 'selected field' window.Now your gridview control will appear like this :

<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"


href="http://4.bp.blogspot.com/_avkw6l8uMYs/Spk0m3bBDSI/AAAAAAAABJE/YI-
HdXCu2mo/s1600-h/untitled2.JPG"><img style="margin: 0px auto 10px; display: block;
text-align: center; cursor: pointer; width: 320px; height: 113px;"
src="http://4.bp.blogspot.com/_avkw6l8uMYs/Spk0m3bBDSI/AAAAAAAABJE/YI-
HdXCu2mo/s320/untitled2.JPG" alt="" id="BLOGGER_PHOTO_ID_5375385472560336162"
border="0"></a>
............Now we will use the
Rowediting, Rowcancelin and Rowupdating events of our gridview control.First of
all select the RowEditing event of your gridview control and the following coding
to it :

<span style="color: rgb(51, 51, 153);">protected void GridView1_RowEditing(object


sender, GridViewEditEventArgs e)</span><br style="color: rgb(51, 51, 153);"><span
style="color: rgb(51, 51, 153);"> {</span><br><br style="color: rgb(51, 51,
153);"><span style="color: rgb(51, 51, 153);"> GridView1.EditIndex =
e.NewEditIndex;</span> <br><br style="color: rgb(51, 51, 153);"><span
style="color: rgb(51, 51, 153);"> BindGrid();</span> <br><br style="color:
rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);"> }</span>

......................Now select the


RowCancelingEdit event and add the following code to it :

<span style="color: rgb(51, 51, 153);">protected void


GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)</span><br
style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);">
{</span><br><br style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51,
153);"> GridView1.EditIndex = -1;</span> <br><br style="color: rgb(51, 51,
153);"><span style="color: rgb(51, 51, 153);"> BindGrid();</span> <br><br
style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);"> }</span>

.............And finaly select the


RowUpdating event and add following code to it :

<span style="color: rgb(51, 51, 153);">protected void GridView1_RowUpdating(object


sender, GridViewUpdateEventArgs e)</span><br style="color: rgb(51, 51,
153);"><span style="color: rgb(51, 51, 153);"> {</span> <br><br style="color:
rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);"> string NameOld =
Convert.ToString(GridView1.DataKeys[e.RowIndex].Value);</span><br><br
style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);">
TextBox Name = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[1];
</span><br><br style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51,
153);"> TextBox Desig =
(TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[1];</span><br><br
style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);">
TextBox Comp =
(TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[1];</span><br><br
style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);">
TextBox Exep = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[1];
</span><br><br style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51,
153);"> con.Open();</span><br><br style="color: rgb(51, 51, 153);"><span
style="color: rgb(51, 51, 153);"> string str = "update Profile set Name = '" +
Name.Text + "', Designation = '" + Desig.Text + "', Company = '" + Comp.Text + "',
Experience = '" + Exep.Text + "' where Name = '" + NameOld + "' ";</span><br><br
style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51, 153);">
SqlCommand com = new SqlCommand(str, con);</span><br><span style="color: rgb(51,
51, 153);"> </span><br style="color: rgb(51, 51, 153);"><span style="color:
rgb(51, 51, 153);"> com.ExecuteNonQuery();</span><br><br style="color: rgb(51,
51, 153);"><span style="color: rgb(51, 51, 153);"> GridView1.EditIndex = -1;
</span><br><br style="color: rgb(51, 51, 153);"><span style="color: rgb(51, 51,
153);"> con.Close();</span><br><br style="color: rgb(51, 51, 153);"><span
style="color: rgb(51, 51, 153);"> BindGrid(); </span><br><br style="color:
rgb(51, 51, 153);"> <br style="color: rgb(51, 51, 153);"><span style="color:
rgb(51, 51, 153);"> }</span>

Now we are ready to run our applcation again.........so just run your
application and click on any edit button.....Gridview will appear like this :

<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"


href="http://3.bp.blogspot.com/_avkw6l8uMYs/Spk1GTdKPsI/AAAAAAAABJM/2yaTHhug4s4/s1
600-h/untitled3.JPG"><img style="margin: 0px auto 10px; display: block; text-
align: center; cursor: pointer; width: 320px; height: 80px;"
src="http://3.bp.blogspot.com/_avkw6l8uMYs/Spk1GTdKPsI/AAAAAAAABJM/2yaTHhug4s4/s32
0/untitled3.JPG" alt="" id="BLOGGER_PHOTO_ID_5375386012661464770" border="0"></a>
...............Now simply make changes and
update/cancel the records according to your choice.In the next part, We will learn
how to add new record to the gridview control and how to delete records from the
gridview control.

.....................contd.

Das könnte Ihnen auch gefallen