Paging in gridview - c#

My gridView:
<asp:GridView ID="gridView1" runat="server" CellPadding="4" AllowPaging="true" PageSize="5" emptydatatext="No data available."
CssClass="datagrid"
ForeColor="#333333" GridLines="None"
onrowcreated="gridView1_RowCreated"
onpageindexchanging="gridView1_PageIndexChanging">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
And my codebehind:
protected void gridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridView1.PageIndex = e.NewPageIndex;
BindDataGrid();
}
protected void BindDataGrid()
{
DataSet ds = new DataSet();
ds = dbM.GetInfo(name, mobilePhone, info1); //get info to BD and save in "ds"
gridView1.DataSource = ds;
gridView1.DataBind();
}
and the "Paging" does not work. It shows the first 5 rows, but does not show the number of paging
What´s wrong???
Please help
thanks

Try adding some pager settings in the GridView tag:
<PagerSettings Mode="NextPreviousFirstLast" Position="TopAndBottom" />
If that works, you can change it to your desired appearance. There are many choices, including
FirstPageImageUrl
FirstPageText
LastPageImageUrl
LastPageText
Mode (such as Numeric or
NumericFirstLast)
NextPageImageUrl
NextPageText
PageButtonCount
Position
PreviousPageImageUrl
PreviousPageText
Visible`

Add the PagerSettings-Property to the GridView-Markup and set it to True:
PagerSettings-Visible="true"
Edit: but it should be true by default, so i'm not sure if this will change anything

Use **"PageIndexChanged"**
protected void gridView1_PageIndexChanged(object sender, GridViewPageEventArgs e)
{
try
{
gridView1.PageIndex = e.NewPageIndex;
BindDataGrid();
;
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

Related

ASP.NET GridView cant display in page load

I have problem in display gridview in my project asp.net .
This is my Gridview.
<asp:GridView ID="GridView1" runat="server" Width="100%" ViewStateMode="Enabled" AutoGenerateColumns="true" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" AutoGenerateSelectButton="true" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
This is my code in page load.
protected void Page_Load(object sender, EventArgs e)
{
try
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = connectionstring;
conn.Open();
string sql = "select * from merchant";
OracleCommand cmd = new OracleCommand(sql, conn);
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error : " + ex.ToString());
Label1.Text = ex.ToString();
}
}
why my Gridview can't display?. Someone please fix my problem.
Thanks
Change Your ASP.NET CODE
Exp:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="ID" DataField="IDUser" ItemStyle-Width="50"/>
<asp:BoundField HeaderText="Name" DataField="Name" ItemStyle-Width="200"/>
<asp:BoundField HeaderText="Username" DataField="UserName" ItemStyle-Width="200"/>
</Columns>
</asp:GridView>

Firing GridView SelectedIndexChanged from code behind

I have a gridview with the implementation of SelectedIndexChanged.
<asp:GridView ID="gvCalloutTeam" runat="server" OnRowDataBound="gvCalloutTeam_RowDataBound" OnSelectedIndexChanged="gvCalloutTeam_SelectedIndexChanged"
AutoGenerateColumns="False" EnableViewState="False" BackColor="White" BorderColor="#cccccc" BorderWidth="1px" CellPadding="2"
EmptyDataText="No person found" GridLines="None" Width="100%">
<AlternatingRowStyle BackColor="#ededed" />
<Columns>
.......... List of columns..............
</Columns>
<FooterStyle BackColor="#cccccc" ForeColor="Black" />
<HeaderStyle BackColor="#6699cc" Font-Bold="True" ForeColor="White" BorderColor="#cccccc" BorderWidth="1px" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#fefefe" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
I have a check box on the page and CheckedChanged Event implemented on that.
<asp:CheckBox ID="cbTakeOutOrder" runat="server" Text="Take out of order" OnCheckedChanged="cbTakeOutOrder_CheckedChanged" AutoPostBack="true" />
I'm trying to set SelectedIndex of GridView from this event.
protected void cbTakeOutOrder_CheckedChanged(object sender, EventArgs e)
{
if (!cbTakeOutOrder.Checked && gvCalloutTeam.Rows.Count > 0)
gvCalloutTeam.SelectedIndex = 0;
}
But it doesn't fire the SelectedIndexChanged event of GridView. If I select the row on UI, it fires but if I change Index in code, it doesn't fire the event. Is there a way to invoke the event, after changing SelectedIndex in code behind?
protected void gvCalloutTeam_SelectedIndexChanged(object sender, EventArgs e)
{
//////////////////my logic
}
If you are on .net 4.5, you can call SelectRow method on the GridView:
protected void cbTakeOutOrder_CheckedChanged(object sender, EventArgs e)
{
if (!cbTakeOutOrder.Checked && gvCalloutTeam.Rows.Count > 0)
gvCalloutTeam.SelectRow(0);
}

By using below code how can i delete image from folder in asp .net

<asp:GridView ID="gvDisplayImages" runat="server" BackColor="#CCCCCC"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" AutoGenerateColumns="false" DataKeyNames="intId"
onrowdeleting="gvDisplayImages_RowDeleting"
onselectedindexchanged="gvDisplayImages_SelectedIndexChanged">
<Columns>
<asp:CommandField SelectText="Delete" ShowDeleteButton="true" />
<asp:BoundField DataField="varImageName" HeaderText="ImageName"
SortExpression="varImageName" />
<asp:TemplateField HeaderText="Preview Image">
<ItemTemplate>
<asp:Image ID ="Img1" runat="server" Height="150" Width="200" ImageUrl='<%#ResolveUrl ("~/" + Eval("varImagePath")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
code for cs page
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
public void BindData()
{
gvDisplayImages.DataSource = bcObj.DisplayImages();
gvDisplayImages.DataBind();
}
protected void gvDisplayImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int intId = Convert.ToInt32(gvDisplayImages.DataKeys[e.RowIndex].Value.ToString());
bcObj.DeleteImage(intId);
BindData();
string filename = Convert.ToString(gvDisplayImages.Rows[e.RowIndex].FindControl("Img1") as Image);
string filepath = "\\Image\\" + filename;
FileInfo file = new FileInfo(filepath);
if (file.Exists)
{
file.Delete();
}
}
Here I am using 3 tier architecture in asp .net By using above code while click delete button in gridview, images are deleted from database successfully. Now please tell me how should I delete image from folder where all images are stored.
Need to use ImageUrl in your code.
like this
string filename = ((Image)(gvDisplayImages.Rows[e.RowIndex].FindControl("Img1"))).ImageUrl;

loading multiple images attachecd to one thumbnail

Hi coder i search a lot but didn't find what i want
i have a database in which i have path of my images.. now i bind images with by gridview and apply lightbox on it but what i want is to show only one image in front as thumbnail and when i click on that images slide show will start..
this is my code:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" PageSize="1" PersistedSelection="true" DatakeyNames="pptId">
<Columns>
<asp:BoundField headerText="pptId" DataField="pptId"></asp:BoundField>
<asp:TemplateField HeaderText="View PPT">
<ItemTemplate>
<a id="imageLink" href='<%# Eval("Imageurl") %>' title='<%#Eval("Description") %>'
rel="lightbox[Brussels]" runat="server">Show PPT</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<SortedAscendingCellStyle BackColor="#F5F7FB"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#6D95E1"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#E9EBEF"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#4870BE"></SortedDescendingHeaderStyle>
</asp:GridView>
this is my aspx.cs page code:
protected void Page_Load(object sender, EventArgs e)
{
BindModule();
}
protected void BindModule()
{
try
{
con.Open();
string id = Request.QueryString["pptId"].ToString();
//Query to get ImagesName and Description from database
SqlCommand command = new SqlCommand("select * from Image_Master where pptId='" + id + "' and IsEnable='True' order by Priority", con);
dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Dispose();
}
catch (Exception ex)
{
Response.Write("Error occured : " + ex.Message.ToString());
}
finally
{
con.Close();
}
problem is that when i bind the gridview it will show all images regarding selected pptid and when i click on any images my sildeshow start but what i want is to show only one image and when i click on that image my slide show start. i use lightbox for slide show and my images comes from database

How to hide and show the DetailsView in the Master-Details GridView & DetailsView?

I am trying to follow and utilize the explained example in ASP.NET website that is about Quiz Engine. I have a Master-Details in the result page where when the user selects one of his answered quesitons in the GridView, the details of that question will be displayed in the DetailsView underneath the GridView. Everything works fine. What I want now is just making the DetailsView (which is the details of the answered question) hidden unless the user selects one of the answered questions. So how to do that?
I set visibility of the DetailsView to false, but I don't know how to hide/show based on the user click on the SELECT option.
My ASP.NET code:
<tr>
<td>
<asp:GridView ID="resultGrid" runat="server" DataKeyNames="QuestionID" SelectedIndex="0"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateSelectButton="True" OnSelectedIndexChanged="resultGrid_SelectedIndexChanged" Width="555px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="QuestionID" HeaderText="Question" />
<%--<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer" />--%>
<asp:BoundField DataField="UserAnswer" HeaderText="Your Answer" />
<asp:BoundField DataField="Result" HeaderText="Result" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" CssClass="boldtext" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [Question], [Answer1], [Answer2], [Answer3], [QuestionID], [QuestionOrder], [Answer4], [CorrectAnswer], [AnswerExplanation], [QuizID] FROM [Question] WHERE ([QuizID] = #QuizID) ORDER BY [QuestionOrder]">
<SelectParameters>
<asp:SessionParameter Name="QuizID" SessionField="QuizID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
<asp:DetailsView ID="answerDetails" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="45px" Width="552px" DataSourceID="SqlDataSource1"
AutoGenerateRows="False" DataKeyNames="QuestionID" Visible="false">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" CssClass="generaltext" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" CssClass="boldtext" Width="100px" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Fields>
<asp:BoundField DataField="Question" HeaderText="Question"
SortExpression="Question" />
<asp:BoundField DataField="Answer1" HeaderText="A"
SortExpression="Answer1" />
<asp:BoundField DataField="Answer2" HeaderText="B"
SortExpression="Answer2" />
<asp:BoundField DataField="Answer3" HeaderText="C"
SortExpression="Answer3" />
<asp:BoundField DataField="Answer4" HeaderText="D"
SortExpression="Answer4" />
<asp:BoundField DataField="CorrectAnswer" HeaderText="Correct Answer"
SortExpression="CorrectAnswer" HeaderStyle-BackColor="lightgreen" />
<asp:BoundField DataField="AnswerExplanation" HeaderText="Explanation"
SortExpression="AnswerExplanation" HeaderStyle-BackColor="lightgreen" />
</Fields>
</asp:DetailsView>
</td>
</tr>
Code-Behind code:
public partial class Results : System.Web.UI.Page
{
Object bShowDetailsView;
protected void Page_Load(object sender, EventArgs e)
{
bShowDetailsView = false;
ArrayList al = (ArrayList)Session["AnswerList"];
if (al == null)
{
Response.Redirect("default.aspx");
}
resultGrid.DataSource = al;
resultGrid.DataBind();
// Save the results into the database.
if (IsPostBack == false)
{
// Calculate score
double questions = al.Count;
double correct = 0.0;
for (int i = 0; i < al.Count; i++)
{
Answer a = (Answer)al[i];
if (a.Result == Answer.ResultValue.Correct)
correct++;
}
double score = (correct / questions) * 100;
string username = HttpContext.Current.User.Identity.Name.ToString().Replace("ARAMCO\\", "");
SqlDataSource userQuizDataSource = new SqlDataSource();
userQuizDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ToString();
userQuizDataSource.InsertCommand = "INSERT INTO [UserQuiz] ([QuizID], [DateTimeComplete], [Score], [Username]) VALUES (#QuizID, #DateTimeComplete, #Score, #Username)";
userQuizDataSource.InsertParameters.Add("QuizID", Session["QuizID"].ToString());
userQuizDataSource.InsertParameters.Add("DateTimeComplete", DateTime.Now.ToString());
// "N4" is for displaying four decimal places, regardless of what the value is
userQuizDataSource.InsertParameters.Add("Score", score.ToString("N4"));
userQuizDataSource.InsertParameters.Add("Username", username);
int rowsAffected = userQuizDataSource.Insert();
if (rowsAffected == 0)
{
// Let's just notify that the insertion didn't
// work, but let' s continue on ...
errorLabel.Text = "There was a problem saving your quiz results into our database. Therefore, the results from this quiz will not be displayed on the list on the main menu.";
}
}
}
protected void resultGrid_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataSource1.FilterExpression = "QuestionOrder=" + resultGrid.SelectedValue;
bShowDetailsView = true;
answerDetails.Visible = bShowDetailsView;
}
}
why not make the first record in resultGrid selected by default because the event you are handling is SelectedIndexChanged but not Selected/Unselected (i dont know whether these are there or not)

Categories