Click here to Skip to main content
11,433,220 members (59,682 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Daer All;

Design Code:

<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 :

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 Smile | :)
Posted 17-Dec-12 0:32am
Edited 17-Dec-12 0:34am
v2
Comments
Tadit Dash at 17-Dec-12 6:27am
   
Have you check with the debugger, whether it is hitting or not?
GANESH KUMAR K at 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..
GANESH KUMAR K at 19-Dec-12 4:14am
   
Thaks for your help @Armando Talex Thank you so much...
GANESH KUMAR K at 21-Dec-12 0:21am
   
Dear Armando Talex , I just Updated my codes fully.. See below... Thank you for your response. And actually the problem was not that one. Eventhough i changed the visibility true i did'n get the selected index changed event firing.. Thanks for your help again.. The code below is working.... Finally i got what exactly i wanted to get.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 Smile | :) 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 Smile | :)

ASPX Code:

<!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 :

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
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100