Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Code
HTML CODE >>
XML
<asp:GridView ID="dgv1" runat="Server" AutoGenerateColumns="false"
          CssClass="mGrid" Height="50px" Width="20px" OnSelectedIndexChanged="OnSelectedIndexChanged"  >
            <Columns>
    <asp:CommandField ShowSelectButton  =  "True"  />
  </Columns>
  <SelectedRowStyle BackColor  =  "#FFCC66" Font-Bold  =  "True"    ForeColor  =  "#663399" />

          </asp:GridView>

C# Coding

 public static DataTable GetDataTable(string sql)
        {
            using (DataSet ds = new DataSet())
            {
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
                string str = ConfigurationManager.ConnectionStrings["ConnMCD"].ToString();
                SqlConnection conn = new SqlConnection(str);
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
dgv1.DataSource = dt;
dgv1.DataBind();
            }
        }

Bound field in Grid with This Code

if (!this.IsPostBack)
           {
               if (option1 != "")
               { }
               BoundField afield = new BoundField();
               afield.HeaderText = option1.ToString();
               afield.DataField = option1.ToString();
               dgv1.Columns.Add(afield);

               BoundField bfield = new BoundField();
               bfield.HeaderText = option2.ToString();
               bfield.DataField = option2.ToString();
               dgv1.Columns.Add(bfield);

               BoundField cfield = new BoundField();
               cfield.HeaderText = option3.ToString();
               cfield.DataField = option3.ToString();
               dgv1.Columns.Add(cfield);

               BoundField dfield = new BoundField();
               dfield.HeaderText = option4.ToString();
               dfield.DataField = option4.ToString();
               dgv1.Columns.Add(dfield);

               BoundField efield = new BoundField();
               efield.HeaderText = option5.ToString();
               efield.DataField = option5.ToString();
               dgv1.Columns.Add(efield);

           }
           dgv1.DataSource = null;
           dgv1.DataSource = dv;
           dgv1.DataBind();
           dgv1.Visible = true;
           dgv1.CssClass = "mGrid";



       }



i want when i select row
call this event

C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    int index = dgv1.SelectedRow.RowIndex;
    string name = dgv1.SelectedRow.Cells[0].Text;
    string country = dgv1.SelectedRow.Cells[1].Text;
    string message = "Row Index: " + index + "\\nName: " + name + "\\nCountry: " + country;
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}



But event does not fire on click on Select in GridView
what shoud i do?
Posted
Updated 28-Nov-14 19:43pm
v2

1 solution

Hi
Just change your code with

XML
<asp:GridView ID="dgv1" runat="Server" AutoGenerateColumns="false"
          CssClass="mGrid" Height="50px" Width="20px" OnSelectedIndexChanged="dgv1_SelectedIndexChanged"  >
            <Columns>
    <asp:CommandField ShowSelectButton  =  "True"  />
  </Columns>
  <SelectedRowStyle BackColor  =  "#FFCC66" Font-Bold  =  "True"    ForeColor  =  "#663399" />

          </asp:GridView>


//in code behind
          protected void dgv1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //You code
        }
 
Share this answer
 
Comments
saimm 29-Nov-14 2:15am    
what change will come on with name???
saimm 29-Nov-14 2:20am    
Realy thanks Dear only Name issue
i have spend 3 hours with this issue (:

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