Solved

C# datagridview click event

Posted on 2013-06-10
2
473 Views
Last Modified: 2013-07-24
I have a datagridview that is read only. I have a column named Status. I want to be able to click a button and whatever row is selected in my datagrid, the value in the status column changes from 0 to 1, or 1 to 0
0
Comment
Question by:imonfireDAMMIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Expert Comment

by:AnuTiji
ID: 39237214
Hi

You can use  OnRowCommand event of gridview. In the event update the database for the status column

Sample Code
<asp:GridView ID="gridView1"
AutoGenerateColumns="False" GridLines="None"
            runat="server"  
            onrowcommand="gridMembersList_RowCommand">
        <Columns>
<ItemTemplate>
<asp:Literal id="ltlStatus" value="<% #Eval("Status") %>" />
</ItemTemplate>
 <ItemTemplate>
            <asp:Button ID="btnChangeStatus"  
            CommandArgument="<% #Eval("PrimaryKey")  %>"
             CommandName="More" runat="server" Text="Change Status" />
        </ItemTemplate>
</Columns>
</asp:GridView>


protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{

    int index = Convert.ToInt32(e.CommandArgument);
   Literal ltrlStatus= (Literal)gridMembersList.Rows[index].FindControl("ltlStatus");
    // Update database

}
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 39238814
Try this

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dgv = sender as DataGridView;
    if (dgv == null)
        return;
    if (dgv.CurrentRow.Selected)
    {
        //do you staff.
    }
}

Open in new window

0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question