Solved

C# datagridview click event

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

732 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