简体   繁体   English

当Checkbox选中/未选中Gridview C#时运行事件

[英]Run an event when Checkbox is Checked/Unchecked Gridview C#

I have a Gridview that has 4 columns. 我有一个具有4列的Gridview。 All in ItemTemplate One column is a CheckBox. ItemTemplate所有内容一栏是一个CheckBox。 This Gridview is binded to a datasource. 此Gridview绑定到数据源。 What I want to do is when the Checkbox is checked update the database with the bit value '1' and if I uncheck the Checkbox to do the same but '0'. 我想做的是选中“复选框”时,用位值“ 1”更新数据库,如果我取消选中“复选框”,则执行相同的操作,但“ 0”。

I already know my SQL code to update the database, but I am not sure how to run the event so that it looks at the current row the checkbox is on in the gridview. 我已经知道我的SQL代码来更新数据库,但是我不确定如何运行该事件,以使其查看gridview中复选框所位于的当前行。

Here is the CheckBox Column. 这是CheckBox列。

<asp:TemplateField  HeaderText="Pick" ItemStyle-CssClass="hrGrid" HeaderStyle-CssClass="hdrHrBase">
<ItemTemplate>
<asp:CheckBox id="cbViewCustomer" runat="server" OnCheckedChanged="ViewCustomer" onAutoPostBack="true" Checked='<%#(Eval("chosen"))%>'/>        
</ItemTemplate>
</asp:TemplateField>

Do I use EventArgs like this: The problem with this is it updates the database but all the rows. 我是否像这样使用EventArgs:问题是它会更新数据库,但会更新所有行。 What I am trying to do is update only the current row that is in the Gridview. 我想做的是仅更新Gridview中的当前行。 I am just not sure where to go from here. 我只是不确定从这里去哪里。

protected void ViewCustomer(object sender, EventArgs e)
    {
        string SelectCustomer = "UPDATE tblcustomer Set chosen ='0'";
        NpgsqlCommand changedata = new NpgsqlCommand(SelectCustomer, con);
        con.Open();
        changedata.ExecuteNonQuery();
        con.Close();

    }

Or should I be doing something different? 还是我应该做些不同的事情?

You can do this with OnRowCommand event of gridview like this:- 您可以使用gridview的OnRowCommand事件来做到这一点:-

Associate a RowCommand event with your gridview:- 将RowCommand事件与您的gridview关联:

<asp:GridView ID="mygridview" runat="server" OnRowCommand="mygridview_OnRowCommand">

Next, associate a CommandName & CommandArgument to your checkbox:- 接下来,将CommandNameCommandArgument关联到您的复选框:

<asp:TemplateField  HeaderText="Pick" ItemStyle-CssClass="hrGrid" >
   <ItemTemplate>
     <asp:CheckBox id="cbViewCustomer" runat="server" Checked='<%#(Eval("chosen"))%>' 
         CommandName="myCheckbox" CommandArgument="<%# Container.DataItemIndex %>"/>
   </ItemTemplate>
</asp:TemplateField>

In the code behind handle the event:- 在处理事件的代码中:

protected mygridview_OnRowCommand (object sender, GridViewCommandEventArgs e)
{
     if(e.CommandName == "myCheckbox")
     {
          int rowIndex = Convert.ToInt32(e.CommandArgument);
          GridViewRow row = mygridview.Rows[rowIndex];
          bool ischecked = (row.FindControl("cbViewCustomer") as CheckBox).Checked;
     }
}

You can also do it with checbox checked event, but if you have multiple controls then fire the gridview command event instead of individual events. 您也可以使用checbox选中的事件来执行此操作,但是如果您有多个控件,则触发gridview命令事件而不是单个事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在C#桌面应用程序中未选中所有子复选框,则是否选中了所有子复选框,也会选中标题中的复选框 - Checkbox in header will be checked if all child checkboxes are checked and unchecked when even one of them is unchecked in C# Desktop Application 在GridView中复选框选中的事件 - Checkbox checked event in gridview 在C#中选中Web浏览器的复选框时引发事件 - Raising event when the checkbox of a webbrowser is checked in C# gridview中的选中复选框,在c#代码中,checked属性为false - Checked checkbox in gridview, in c# codebehind checked property is false 如何获取 datetimepicker c# winform 选中/未选中事件 - How to get datetimepicker c# winform checked/unchecked event C#DataGridView复选框已选中事件 - C# DataGridView Checkbox checked event C#-添加新行时重新加载gridview时,请选中此复选框 - C# - Keep the checkbox checked when gridview reloads when adding new row 当复选框未选中或发生事件时,如何通知多个视图模型? C# WPF - How to notify more than one View models when checkbox is unchecked or on an event? C# WPF C#检查复选框是否选中(也许用户选中并取消选中了同一复选框) - C# check if checkbox check(maybe user checked and unchecked a same checkbox) CheckBox在c#GridView中的第二次单击中被选中 - CheckBox gets checked in the second click in c# GridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM