vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Tuesday, February 1, 2011

GRID VIEW LOAD AND UPDATE SQL DATA TABLE INTO GRID VIEW USING ASP.NET




hi friends..

Here i describe the methods to load sql data table into grid view.By using this article you will get clear idea to load the table into grid view also you can also update the table using grid view..


--------This are Grid view coding are comes under client side ...........
[CODE]
 <asp:GridView ID="GridView1" runat="server"
            CellPadding="3" GridLines="Vertical"
            AutoGenerateColumns="False" Height="155px" Width="573px" BackColor="White"
                BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
                AllowPaging="True" DataKeyNames="Number">
                    <Columns >
        <asp:TemplateField HeaderText ="PHONE NUMBER">
                        <ItemTemplate >
                        <%#Eval("Number")%>
                        </ItemTemplate>
        </asp:TemplateField>
      
        <asp:TemplateField HeaderText ="NAME" >
                        <ItemTemplate >
                         <%#Eval("Name")%>
                         </ItemTemplate>
                         <EditItemTemplate >
                         <cc1:textbox ID="txtrwname" runat="server" Text ='<%#Bind("Name")%>'></cc1:textbox>
                             </EditItemTemplate>
                </asp:TemplateField>
      
        <asp:TemplateField  HeaderText ="ADDRESS">
                        <ItemTemplate >
                        <%#Eval("Address")%>      
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Left" />    
                           <EditItemTemplate >
                            <cc1:textbox ID="txtrwaddress" runat="server" Text='<%#bind("Address")%> ' Validate ="IsAlphaNum" ></cc1:textbox>
                            </EditItemTemplate>  
                </asp:TemplateField>
        <asp:TemplateField HeaderTEXT="CITY" >
                        <ItemTemplate >
                        <%#Eval("City")%>
                        </ItemTemplate>      
                           <EditItemTemplate>
                               <cc1:textbox ID="txtrwcity" runat="server" Text='<%#bind("City")%>'></cc1:textbox>
                                </EditItemTemplate>  
                </asp:TemplateField>
            <asp:TemplateField HeaderText ="STATE">
                        <ItemTemplate>
                         <%#Eval("State")%>
                        </ItemTemplate>
                          <EditItemTemplate>
                           <cc1:textbox ID="txtrwstate" runat="server" Text='<%#bind("State")%> ' Validate ="IsAlphaNum ">
                                    <cc1:textbox>
                                      </EditItemTemplate>
    </asp:TemplateField>
[/CODE]
---------This field are used to modify and update the database---------------------


  <asp:CommandField HeaderText ="modify" ShowDeleteButton ="false" ShowEditButton ="true" EditText ="modify" />
       <asp:TemplateField HeaderText="Delete">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick ="return confirm('are you sure')">Delete</asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
  </Columns>  
 </asp:GridView>

---In server side you have to add this coding by using grid view properties------

  Sub loadde()
        fselect = "select * from phone"
        'sselect = "select *from phone where Number='" & txtPhonno.Text & "'"
        'r1table = obj.ExecuteData(sselect)
        rtable = obj.ExecuteData(fselect)


        GridView1.DataSource = rtable
        GridView1.DataBind()




You can find this event in Grid View properties......

Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        loadde()
    End Sub


Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        loadde()


    End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        loadde()


    End Sub


 Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
           phonercd = GridView1.DataKeys(e.RowIndex).Value.ToString
        fselect = "delete phone where Number='" & phonercd & "'"
        obj.ExecuteCommand(fselect)
        loadde()
    End Sub
  
 Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
                fselect = "update phone set Name='" & Trim(rwname.text) & "',Address='" & Trim(rwaddress.Text) & "',City='" & Trim(rwcity.Text) & "',State='" & Trim(rwstate.text) & "' where Number='" & Trim(phonercd) & "'"
        obj.ExecuteCommand(fselect)
        GridView1.EditIndex = -1
        loadde()
    End Sub
  
plz post your valuable comments.....
  


1 comments:

Muhammad Azeem said...

This is a nice article..
Its easy to understand ..
And this article is using to learn something about it..

c#, dot.net, php tutorial, Ms sql server

Thanks a lot..!
ri80

Post a Comment