[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

C# datagridview click event

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
imonfireDAMMIT
Asked:
imonfireDAMMIT
1 Solution
 
AnuTijiCommented:
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
 
Manoj PatilSr. Software EngineerCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now