Solved

C# datagridview click event

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 debugging 2 35
SQL Login 17 38
Wav problem 4 13
Close form "before" open 3 24
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

21 Experts available now in Live!

Get 1:1 Help Now