locked
RowEditing in GridView in c# RRS feed

  • Question

  • User1872364149 posted

    Hi there,

    I have problem using RowEditing on GridView in c#

    With this ImageButton I selected in GridView only rows where my email address is registered :

    <asp:ImageButton ID="btnOnly" 
         ImageUrl="/images/show_icon.gif" Visible="true"
         runat="server" OnClick="btnOnly_Click"
         CommandArgument="1" CommandName="1" />
    
        protected void btnOnly_Click(object sender, ImageClickEventArgs e)
        {
            btn = (ImageButton)sender;
            CommandName = btn.CommandName;
            CommandArgument = btn.CommandArgument;
            BindData();
        }
    
    SELECT * FROM doTable WHERE doEmail IN ('myemail') ORDER BY ID DESC;

    In this new view of GridView when I selected row for Edit :

    <asp:ImageButton ID="imgbtnEdit" 
         CommandName="Edit" runat="server"
         ImageUrl="/Images/edit.gif" />

    In GridView is opened for edit the row of first view.

    The first row where there are other email addresses other than my email address:

    protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
    {
            gvProducts.EditIndex = e.NewEditIndex;
            BindData();
    }

    Can you help me?

    Thank you in advance for any help, really appreciated.

    Monday, October 16, 2017 10:45 AM

Answers

  • User-707554951 posted

    Hi  cms9651,

    Form your description and code, it is hard for me to understand your problem.

    Would you please provided more detailed description about you problem with complete code?

    Following is a working  sample I used to test, it works well.

    You could take a look:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
                     DataKeyNames="Id" OnRowEditing="GridView1_RowEditing">
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:ImageButton ID="btnOnly"
                                    ImageUrl="~/images/thumb-1.gif" Visible="true"
                                    runat="server" 
                                    CommandArgument="1" CommandName="Edit" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    CodeBehind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    BindData();
                }
            }
            protected void BindData() {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Group"), new DataColumn("Name"), new DataColumn("Country") });
                dt.Rows.Add(1, "A", "John Hammond", "United States");
                dt.Rows.Add(2, "B", "Mudassar Khan", "India");
                dt.Rows.Add(3, "A", "Suzanne Mathews", "France");
                dt.Rows.Add(4, "B", "Robert Schidner", "Russia");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            protected void btnOnly_Click(object sender, ImageClickEventArgs e)
            {
                ImageButton btn = (ImageButton)sender;
               string CommandName = btn.CommandName;
                string CommandArgument = btn.CommandArgument;
               
            }
    
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                BindData();
            }

    Best Regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 17, 2017 3:05 AM