Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear experts,
i had done the gridview but output is coming wrong..plz can any one help me..
this is my source code , aspx.cs code and output also
source
XML
<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="false"
            AllowPaging="true" PageSize="5"
             OnRowEditing="gvdetails_RowEditing" OnRowDeleting="gvdetails_RowDeleting" DataKeyNames="UserId,UserName">

        <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="Firstpage" NextPageText="Nextpage" />
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.png" ToolTip="Edit" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/Delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
        </ItemTemplate>
        </asp:TemplateField>

       <asp:TemplateField HeaderText="UserName">
       <ItemTemplate>
       <asp:Label ID="lblitmusr" runat="server" Text='<%Eval("UserName")%>'></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="City">
       <ItemTemplate>
       <asp:Label ID="lblitmcity" runat="server" Text='<%Eval("City") %>' ></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Designation">
       <ItemTemplate>
       <asp:Label ID="lblitmdesignation" runat="server" Text='<%Eval("Designation")%>'></asp:Label>
       </ItemTemplate>
       </asp:TemplateField>

        </Columns>

        </asp:GridView>
        <asp:Label ID="lblresult" runat="server"></asp:Label>


aspx.cs
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["grid"].ConnectionString);
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }



    }
    protected void BindGridView()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from GridDetails", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvdetails.DataSource = ds;
            gvdetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvdetails.DataSource = ds;
            gvdetails.DataBind();
            int columncount = gvdetails.Rows[0].Cells.Count;
            gvdetails.Rows[0].Cells.Clear();
            gvdetails.Rows[0].Cells.Add(new TableCell());
            gvdetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvdetails.Rows[0].Cells[0].Text = "No Records Found";

        }


    }
    protected void gvdetails_RowEditing(object sender,GridViewEditEventArgs e )
    {
        gvdetails.EditIndex = e.NewEditIndex;
        gvdetails.DataBind();
    }
    protected void gvdetails_RowDeleting(object sender,GridViewDeleteEventArgs e)
    {
        
        
        int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
        string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from GridDetails where UserId=" + userid, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindGridView();
            lblresult.Text = username + "details deleted successfully";

        }
    }

Output
XML
UserName               City                 Designation
       <%Eval("UserName")%>    <%Eval("City") %>   <%Eval("Designation")%>
   <%Eval("UserName")%>    <%Eval("City") %>   <%Eval("Designation")%>
   <%Eval("UserName")%>    <%Eval("City") %>   <%Eval("Designation")%>
Posted
Updated 6-Sep-12 3:01am
v2
Comments
Sandeep Mewara 6-Sep-12 12:15pm    
You failed to mention what is "wrong output"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900