Pages

Subscribe:

Sunday, 18 January 2015

Find Row Index in gridView

 DataKeyNames="id"
<Columns>
                                                <asp:TemplateField HeaderText="NO." HeaderStyle-HorizontalAlign="Left" Visible="false">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lblSNo" runat="server" Text='<%# Eval("SrNo")%>'></asp:Label>
                                                        <asp:Label ID="hdSeq" runat="server" Text='<%# Eval("ItemOrder")%>' Visible="false" />
                                                    </ItemTemplate>
                                                    <ItemStyle HorizontalAlign="Left" />
                                                </asp:TemplateField>
                                                <asp:BoundField DataField="Press_Title" HeaderText="Press Release" HeaderStyle-HorizontalAlign="Left"
                                                    ItemStyle-HorizontalAlign="Left" />
                                                <%--<asp:ButtonField CommandName="edt" HeaderText="Edit" Text="Edit" HeaderStyle-HorizontalAlign="Left" />--%>
                                                <asp:TemplateField HeaderText="Edit" HeaderStyle-Width="10%" ItemStyle-HorizontalAlign="Center">
                                                    <ItemTemplate>
                                                        <asp:LinkButton ID="btnedit" runat="server" CommandName="edt" CommandArgument='<%# Eval("specialid") %>'
                                                            CausesValidation="False">Edit</asp:LinkButton>
                                                    </ItemTemplate>
                                                    <HeaderStyle HorizontalAlign="Center" />
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Status">
                                                    <ItemTemplate>
                                                        <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("Press_status") %>'
                                                            CommandName="Change" CommandArgument='<%# Eval("specialid") %>' CausesValidation="False"
                                                            OnClick="LinkButton1_Click"></asp:LinkButton>
                                                    </ItemTemplate>
                                                    <HeaderStyle HorizontalAlign="Left" />
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Up" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Left">
                                                    <ItemTemplate>
                                                        <asp:LinkButton ID="lnkup" CausesValidation="false" runat="server" CommandName="Up"
                                                            Visible="false">
                                                            <asp:Image ID="imgUp" runat="server" ImageUrl="images/up_red.gif" />
                                                        </asp:LinkButton>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Down" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Left">
                                                    <ItemTemplate>
                                                        <asp:LinkButton ID="lnkdown" CausesValidation="false" runat="server" CommandName="Down"
                                                            Visible="false">
                                                            <asp:Image ID="imgDown" runat="server" ImageUrl="images/down_red.gif" />
                                                        </asp:LinkButton>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>

 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton lb1 = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lb1.NamingContainer;
        Session["Index"] = row.RowIndex;
    }



if (e.CommandName == "edt" || e.CommandName == "Change" || e.CommandName == "Up" || e.CommandName == "Down")
            {
                GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

                int index = Convert.ToInt32(row.RowIndex);
                string sProdId = GridView1.DataKeys[index].Value.ToString();
                if (e.CommandName == "Change")
                {
                    Session["spcloffer"] = e.CommandArgument;
                }
                else if (e.CommandName == "edt")
                {
                    Session["spcloffer"] = Convert.ToString(e.CommandArgument);
                    using (SqlCommand cmd = new SqlCommand("Sp_ManageExhibitorPress", Conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        using (SqlDataAdapter da = new SqlDataAdapter())
                        {
                            da.SelectCommand = cmd;
                            da.SelectCommand.Parameters.AddWithValue("@eventid", Session["eventid"].ToString());
                            da.SelectCommand.Parameters.AddWithValue("@exid", Convert.ToString(Session["exid"]));
                            da.SelectCommand.Parameters.AddWithValue("@specialId", Convert.ToString(Session["spcloffer"]));
                            da.SelectCommand.Parameters.AddWithValue("@flag", "GRID_MODIFY");
                            da.SelectCommand.Parameters.AddWithValue("@RecordCount", 0);
                            da.SelectCommand.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                            //Response.Write("test-" + Session["eventid"].ToString() + "-" + Session["exid"].ToString() + "-" + Convert.ToString(Session["spcloffer"]));
                            using (DataTable dt = new DataTable())
                            {
                                da.Fill(dt);
                                if (dt != null && dt.Rows.Count > 0)
                                {
                                    lblSuccess.Text = "";
                                    txtProductName.Text = dt.Rows[0]["Press_Title"].ToString();
                                    txtProductDescription.Text = dt.Rows[0]["Press_desc"].ToString();
                                    if (!string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["Press_pdf"])))
                                    {
                                        Pdf.HRef = Convert.ToString(dt.Rows[0]["Press_pdf"]);
                                        Pdf.InnerText = "View";
                                    }


                                }
                                btnSubmit.Visible = false;
                                btnUpdate.Visible = true;
                            }
                        }
                    }
                }

                switch (e.CommandName.ToLower())
                {

                    case "change":

                        LinkButton lb = (LinkButton)GridView1.Rows[Convert.ToInt32(Session["Index"].ToString())].Cells[0].FindControl("LinkButton1");
                        string status = string.Empty;
                        if (lb.Text == "Valid")
                        {
                            status = "Invalid";
                        }
                        else
                        {
                            status = "Valid";
                        }
                        using (SqlCommand cmd = new SqlCommand("Sp_ManageExhibitorPress", Conn))
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@specialId", Convert.ToString(Session["spcloffer"]));
                            cmd.Parameters.AddWithValue("@eventid", Session["eventid"].ToString());
                            cmd.Parameters.AddWithValue("@exid", Session["exid"].ToString());
                            cmd.Parameters.AddWithValue("@special_status", status);
                            cmd.Parameters.AddWithValue("@flag", "UPDATE_STATUS");
                            cmd.Parameters.AddWithValue("@RecordCount", 0);
                            cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                            Conn.Open();
                            cmd.ExecuteNonQuery();
                            if (cmd.Parameters["@RecordCount"].Value != DBNull.Value)
                            {
                                Conn.Close();
                                lblSuccess.Text = "Records Modified Successfully!";
                            }
                            else
                            {
                                lblSuccess.Text = "Seems Some Problem! Please try again later.";
                            }
                        }
                        break;
                    case "up":
                        {
                            Label Newhd = (Label)GridView1.Rows[index - 1].FindControl("hdSeq");
                            Label hd = (Label)GridView1.Rows[index].FindControl("hdSeq");
                            int NewSeqno = Convert.ToInt32(Newhd.Text);
                            int Seqno = Convert.ToInt32(hd.Text);
                            string sNewProdId = GridView1.DataKeys[index - 1].Value.ToString();
                            using (SqlCommand cmd = new SqlCommand("Sp_ManageExhibitorPress", Conn))
                            {
                                ///<summary>
                                ///this procedure uses table tblProds to update the order in the
                                /// products will be displayed for this exhibitors
                                ///</summary>
                                cmd.CommandType = CommandType.StoredProcedure;
                                cmd.Parameters.AddWithValue("@eventid", Session["eventid"].ToString());
                                cmd.Parameters.AddWithValue("@exid", Session["exid"].ToString());
                                cmd.Parameters.AddWithValue("@sequenceNo", Seqno);
                                cmd.Parameters.AddWithValue("@newSequenceNo", NewSeqno);
                                cmd.Parameters.AddWithValue("@specialId", sProdId);
                                cmd.Parameters.AddWithValue("@newspecialId", sNewProdId);
                                cmd.Parameters.AddWithValue("@flag", "GRID_UP");
                                cmd.Parameters.AddWithValue("@RecordCount", 0);
                                cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                                Conn.Open();
                                cmd.ExecuteNonQuery();
                                Conn.Close();
                            }
                        }
                        break;
                    case "down":
                        {
                            Label Newhd = (Label)GridView1.Rows[index + 1].FindControl("hdSeq");
                            Label hd = (Label)GridView1.Rows[index].FindControl("hdSeq");
                            int NewSeqno = Convert.ToInt32(Newhd.Text);
                            int Seqno = Convert.ToInt32(hd.Text);
                            string sNewProdId = GridView1.DataKeys[index + 1].Value.ToString();
                            using (SqlCommand cmd = new SqlCommand("Sp_ManageExhibitorPress", Conn))
                            {
                                ///<summary>
                                ///this procedure uses table tblProds to update the order in the
                                /// products will be displayed for this exhibitors
                                ///</summary>
                                cmd.CommandType = CommandType.StoredProcedure;
                                cmd.Parameters.AddWithValue("@eventid", Session["eventid"].ToString());
                                cmd.Parameters.AddWithValue("@exid", Session["exid"].ToString());
                                cmd.Parameters.AddWithValue("@sequenceNo", Seqno);
                                cmd.Parameters.AddWithValue("@newSequenceNo", NewSeqno);
                                cmd.Parameters.AddWithValue("@specialId", sProdId);
                                cmd.Parameters.AddWithValue("@newspecialId", sNewProdId);
                                cmd.Parameters.AddWithValue("@flag", "GRID_UP");
                                cmd.Parameters.AddWithValue("@RecordCount", 0);
                                cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
                                Conn.Open();
                                cmd.ExecuteNonQuery();
                                Conn.Close();
                            }
                        }
                        break;
                }
                BindGridData();
            }
        }

0 comments:

Post a Comment