Asked by:
Gridview method to show data (asp.net c#)

Question
-
User-2094959909 posted
Hello everyone,
i have this code to show all my users in th gridview and it works very well
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binData();
}
}public void binData()
{
String strConnString = ConfigurationManager
.ConnectionStrings["parallelConnectionString"].ConnectionString;
MySqlConnection con = new MySqlConnection(strConnString);
MySqlDataAdapter da = new MySqlDataAdapter("select name, surname, username, role from tblusers", con);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "tblusers");
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch(Exception e)
{
Response.Write(e.Message);
}
finally
{
ds.Dispose();
da.Dispose();
con.Dispose();
}
}------------------
in my database i have just 4 users but the question is work on a project that it will have more than 50 user
so if anybody knows how i should done it by a scrollbar or showing users 10 by 10 or something else
ps : i have just one more week to finish my project if someone can help i will be grateful !!!
Thanks in advance.
Thursday, July 19, 2018 10:27 AM
All replies
-
User475983607 posted
Paging and sorting is fundamental feature of the GridView and well documented. The GridView reference documentation has all of this type of information with code examples. I suggest that you start by reading the docs and ask questions if you run into trouble.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview(v=vs.110).aspx
Thursday, July 19, 2018 11:19 AM -
User-2094959909 posted
I deleted it and put a new gridview and i connected it with database so it has given me the data and shown the users
I added two new columns "edit" and "delete" but still don't know how to code them to edit a user or delete itThursday, July 19, 2018 2:05 PM -
User-1171043462 posted
I added two new columns "edit" and "delete" but still don't know how to code them to edit a user or delete itRefer Step by Step Tutorial for performing Insert Edit Update and Delete in GridView in ASP.Net
Thursday, July 19, 2018 7:36 PM -
User-330142929 posted
Hi Omar27,
in my database i have just 4 users but the question is work on a project that it will have more than 50 user
so if anybody knows how i should done it by a scrollbar or showing users 10 by 10 or something else
From your description, I make a demo about how to sort and page record. wish it is useful to you.
Aspx.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="True" AllowSorting="True" PageSize="5" OnSorting="GridView1_Sorting" OnPageIndexChanging="GridView1_PageIndexChanging"> <Columns> <asp:TemplateField HeaderText="Id" SortExpression="Id"> <ItemTemplate> <h4><%# Eval("Id") %></h4> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name" SortExpression="Name"> <ItemTemplate> <h4><%# Eval("Name") %></h4> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price" SortExpression="Price"> <ItemTemplate> <h4><%# Eval("Price") %></h4> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Code behind.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["sortdirection"] = "ASC"; Session["LastSort"] = "Id ASC"; Sort(); } } protected DataSet GetDataSet() { string path = Server.MapPath("~/App_Data/1.xml"); DataSet ds = new DataSet(); ds.ReadXml(path, XmlReadMode.InferSchema); return ds; } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { if (GetDataSet()!=null) { if (ViewState["sortdirection"].ToString()=="ASC") { ViewState["sortdirection"] = "DESC"; Session["LastSort"]= e.SortExpression + " "+ViewState["sortdirection"]; } else { ViewState["sortdirection"] = "ASC"; Session["LastSort"]= e.SortExpression + " "+ViewState["sortdirection"]; } Sort(); } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; Sort(); } public void Sort() { if (GetDataSet()!=null) { DataView dv = new DataView(GetDataSet().Tables[0]); dv.Sort = Session["LastSort"].ToString(); GridView1.DataSource = dv; GridView1.DataBind(); } }
How it works.
Feel free to let me know if you have any questions.
Best Regards
Abraham
Friday, July 20, 2018 6:48 AM