Solved

C# datagridview click event

Posted on 2013-06-10
2
460 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now