Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Daer All;

Design Code:

XML
<asp:GridView ID="grdView" runat="server" DataKeyNames="ID" AutoGenerateColumns="false"
                    OnSelectedIndexChanged="grdView_SelectedIndexChanged">
                    <Columns>
                        <asp:BoundField DataField="ID" Visible="false" HeaderText="ID"></asp:BoundField>
                        <asp:BoundField DataField="NAME" Visible="true" HeaderText="NAME"></asp:BoundField>
                    </Columns>
                </asp:GridView>


C# Code Behind :

C#
protected void grdView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string index = grdView.DataKeyNames.ToString();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }



Once I'm Clicking the gridview The event Selectedindex changed is not getting fired.. Kindly guide me to get out of it..

And If you can give me a example to retrieve data of particular row which I'm selecting from grid Pls.. thanks in advance :-)
Posted
Updated 16-Dec-12 23:34pm
v2
Comments
Have you check with the debugger, whether it is hitting or not?
GANESH KUMAR K 17-Dec-12 6:31am    
Ya i put a break point in the event.. In page load also i do have a break point but it's not entering into pageload also while clicking(Selecting) row in grid..
[no name] 17-Dec-12 6:43am    
Try changing the <asp:BoundField DataField="ID" Visible="false" HeaderText="ID"> to
<asp:BoundField DataField="ID" Visible="true" HeaderText="ID">

if this also not works then there must be some other problems with your code.
you need to attach your whole code so that we can look into the exact problem,
GANESH KUMAR K 19-Dec-12 4:14am    
Thaks for your help @Armando Talex Thank you so much...
[no name] 19-Dec-12 5:02am    
Did that worked?

You need to have a button firing select command in the gridview by default clicking a row does not fire this event. check this link out http://stackoverflow.com/questions/8358641/why-doesnt-my-gridview-selectedindexchanged-event-fire[^]

Regards
Pawan
 
Share this answer
 
This below Codings is to retrieve a data from ASP Datagrid. Those who are beginers to ASP.net this code will be very much useful to know.Please revert me if you facing any problem with this code.Let me try to help you :-)Please don't hesitate to go through all the codes..Hope just a copy paste of these codes in corresponding places will be works by changing your own connection string..Thanks :-)

ASPX Code:

XML
<!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 runat="server">
    <title>Untitled Page</title>

    <script language="javascript" type="text/javascript">
        function DO(ID) {
            document.getElementById("hdnID").value = ID;
            document.getElementById("form1").submit();
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="100%">
            <tr>
                <td colspan="2" align="center">
                    <asp:DataGrid ID="grdview" runat="server" AutoGenerateColumns="false" OnItemDataBound="ItemBound">
                        <Columns>
                            <asp:BoundColumn DataField="ID" HeaderText="ID" Visible="false"></asp:BoundColumn>
                            <asp:BoundColumn DataField="NAME" HeaderText="NAME"></asp:BoundColumn>
                        </Columns>
                    </asp:DataGrid>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                    <input type="hidden" id="hdnID" runat="server" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>



C# Code:

public partial class Ganesh : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
try
{
con = new SqlConnection(@"data source=GANESH-VAIO;initial catalog=GANESH;user id=sa;password=cool");
if (!IsPostBack)
{
FillGrid();
}
else
{
if (!string.IsNullOrEmpty(hdnID.Value))
{
GetValue();
ViewState["ID"] = hdnID.Value;
hdnID.Value = "";
}
}
}
catch (Exception ex)
{
Response.Redirect(ex.Message);
}
}

public void GetValue()
{
DataSet ds;
try
{
ds = new DataSet();
con.Open();
SqlDataAdapter dr = new SqlDataAdapter("select id,name from study where id='" + hdnID.Value.Trim() + "'", con);
dr.Fill(ds);
if(ds!=null && ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
{
txtName.Text = ds.Tables[0].Rows[0]["NAME"].ToString();
}
}
catch (Exception ex)
{
throw ex;
}
}

protected void ItemBound(object sender, DataGridItemEventArgs e)
{
TableRow dr;
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
dr = (TableRow)e.Item.Cells[0].Parent;
dr.Attributes.Add("OnClick", "DO('" + e.Item.Cells[0].Text + "');");
}
}
catch (Exception ex)
{
Response.Redirect(ex.Message);
}
}

protected void FillGrid()
{
DataSet ds;
try
{
ds = new DataSet();
con.Open();
//SqlDataAdapter dr = new SqlDataAdapter("select id,name from study", con);
//dr.Fill(ds);
//grdview.DataSource = ds;
SqlCommand cmd = new SqlCommand("select id,name from study", con);
SqlDataReader read = cmd.ExecuteReader();
grdview.DataSource = read;
grdview.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
}


DATABASE CODE :

SQL
CREATE TABLE STUDY(ID CHAR(16),NAME VARCHAR(100))
INSERT INTO STUDY (ID,NAME) VALUES (REPLACE((RIGHT(NEWID(),17)),'-',''),'GANESH')
INSERT INTO STUDY (ID,NAME) VALUES (REPLACE((RIGHT(NEWID(),17)),'-',''),'RAMESH')
INSERT INTO STUDY (ID,NAME) VALUES (REPLACE((RIGHT(NEWID(),17)),'-',''),'SURESH')
SELECT * FROM STUDY



Thanks & Regards,
Ganesh kumar K
 
Share this answer
 

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