Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is code which making problem after click on edit link in grid view
please kindly solve my problem I will be tooooo much thankful and God will give u fruit about this.
Thanks, and Waiting for ur reply......................





XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="GenderDataSource" Runat="server"
           SelectCommand="SELECT [genderId], [gender] FROM [Gender]"


            ConnectionString="<%$ ConnectionStrings:Example_DB_CheckConnectionString %>">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" Runat="server"
            DataSourceID="SqlDataSource1" DataKeyNames="std_id"
            AutoGenerateColumns="False" AllowPaging="True"
            BorderWidth="1px" BackColor="White"
            CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC">
            <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
            <PagerStyle ForeColor="#000066" HorizontalAlign="Left"
               BackColor="White"></PagerStyle>
            <HeaderStyle ForeColor="White" Font-Bold="True"
               BackColor="#006699"></HeaderStyle>
            <Columns>
                <asp:CommandField ShowEditButton="True"></asp:CommandField>
                <asp:BoundField ReadOnly="True" HeaderText="std_id"
                  InsertVisible="False" DataField="std_id"
                    SortExpression="std_id">
                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField HeaderText="std_name"
                  DataField="std_name"
                  SortExpression="std_name"></asp:BoundField>
               <asp:TemplateField SortExpression="gender"
                    HeaderText="gender">
                    <EditItemTemplate>
                        <asp:DropDownList ID="gender" runat="server" DataSourceID="GenderDataSource"  DataTextField="gender"
                   DataValueField="genderId"
                     SelectedValue='<%#Bind("genderId")%>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label Runat="server"
                       Text='<%# Bind("gender") %>'
                       ID="Label1"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <SelectedRowStyle ForeColor="White"
                Font-Bold="True" BackColor="#669999"></SelectedRowStyle>
            <RowStyle ForeColor="#000066"></RowStyle>
        </asp:GridView>

        <br />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:Example_DB_CheckConnectionString2 %>"
            SelectCommand="SELECT student.std_id, student.std_name, student.std_fname, student.std_nic, Gender.gender FROM student INNER JOIN Gender ON student.genderId = Gender.genderId" UpdateCommand="UPDATE       student
SET                std_name =@std_nam, std_fname =@std_fname, std_nic =@std_nic, genderId =@genderId
where std_id=@std_id">
            <UpdateParameters>
                <asp:Parameter Name="std_nam" />
                <asp:Parameter Name="std_fname" />
                <asp:Parameter Name="std_nic" />
                <asp:Parameter Name="genderId" />
                <asp:Parameter Name="std_id" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <br />
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
        <br />
        <br />
        <br />
        <br />
        <br />

    </div>
    </form>
</body>
</html>
Posted

1 solution

I would implement the OnRowDataBound and OnRowEditing events on the gridview. Inside of your gridview declaration you will have to set these. Which would look like OnRowDataBound="GridView1_RowDataBound" . Then for your code behind portion create a fucntion for each of the events like so,
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
}


C#
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}


Inside of each you will have to do different things depending upon your use case. But, inside of the RowEditing event you will need to set GridView1.EditIndex = e.NewEditIndex;, so that the gridview contains the corresponding index of the row to edit.

Inside of the RowDataBound insert code like,

C#
if (e.Row.RowType == DataControlRowType.DataRow)
{
     if (GridView1.EditIndex == e.Row.RowIndex)
     {
         //Bind your data to your drop down list here
         //To obtain the drop down list control if you 
         //need it, use the following code
         DropDownList editGenderValue = 
                     (DropDownList)e.Row.FindControl("gender");


     }
}


This is a very short answer to your question that I hope points you in the right direction. If you need more help with implementing this please let me know. Also, there are a few really good articles on code project about how to implement this type of solution in a gridview.

Best of luck and please let me know if you get stuck on anything.
 
Share this answer
 
v2
Comments
Sandeep Mewara 7-May-11 14:29pm    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900