Showing posts with label GridView. Show all posts
Showing posts with label GridView. Show all posts
Friday, January 16, 2009
Wednesday, June 25, 2008
GridView ObjectDataSource Paging
Took me a lot of time to figure it out...
First, paging doesn't seem to work when ObjectDataSource Select function returns SqlDataReader so it returns DataView now.
The code looks like this:
1: [DataObjectMethod(DataObjectMethodType.Select)]2: public static DataView GetRows(int statusid, int experienceid, string submitter, string idsid, string SortExpression)
3: { 4: 5: StringBuilder strSql = MainSelectSql(idsid, statusid, experienceid, submitter, SortExpression); 6: 7: SqlConnection connection = Connection.GetConnection();8: SqlCommand command = new SqlCommand(strSql.ToString(), connection);
9: command.CommandType = CommandType.Text;10: SqlDataAdapter adapter = new SqlDataAdapter(command);
11: 12: //connection.Open();
13: DataTable dt = new DataTable();
14: adapter.Fill(dt); 15: connection.Close();16: int totalRecords = dt.Rows.Count;
17: 18: DataView dw = new DataView(dt);
19: 20: return dw;
21: }
The GridView has to have its "AllowPaging" set to true and PageIndexChanging method looks like this:
1: protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
2: { 3: GridView gv = (GridView)sender; 4: gv.PageIndex = e.NewPageIndex; 5: gv.DataBind(); 6: }
Also can change the page size in properties...
Wednesday, May 14, 2008
Add custom row to GridView
1: public partial class _Default : System.Web.UI.Page
2: {3: static int pgSize;
4: 5: protected void Page_Load(object sender, EventArgs e)
6: {7: if(!Page.IsPostBack)
8: pgSize = 0; 9: } 10: 11: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
12: {13: if (e.Row.RowType == DataControlRowType.DataRow)
14: {15: TableCell tCell = new TableCell();
16: // create image
17: Image img = new Image();
18: img.ImageUrl = "subheader.jpg";
19: // add the image to the cell
20: tCell.Controls.Add(img); 21: 22: GridView gView = (GridView)sender;23: // set the colspan to occupy the other cells in the row
24: int colSpan = gView.Columns.Count;
25: tCell.Attributes["ColSpan"] = colSpan.ToString();
26: 27: GridViewRow gRow = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
28: // add the cells to the gridviewrow
29: gRow.Cells.Add(tCell); 30: 31: Table tbl = (Table)e.Row.Parent; 32: 33: // set the pagesize initially to the pagecount/2
34: // in our case it is 10/2 = 5. So the first image will
35: // displayed after the 5th row.
36: if(pgSize == 0)
37: pgSize = GridView1.PageCount / 2; 38: 39: // This step is performed so that we can display the image only after every
40: // 5, 15, 25 ,35 rows and so on ...
41: // The logic is not perfect but will give you the idea
42: if (Convert.ToDouble(e.Row.DataItemIndex + 1) / Convert.ToDouble(pgSize) == 1.0)
43: { 44: tbl.Controls.AddAt(gView.Controls[0].Controls.Count, gRow);45: // add 10 to the pgsize so that the image can be displayed
46: // at rows 5, 15, 25 and so on..
47: pgSize = pgSize + 10; 48: } 49: } 50: } 51: }Saturday, March 22, 2008
Export GridView To Excel
protected void Button1_Click(object sender, EventArgs e) { Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); Response.Charset = String.Empty; Response.ContentType = "application/vnd.xls"; System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.RenderControl(hw); Response.Write(sw.ToString()); Response.End(); }
Paging and Sorting in GridView without using Datasource control
<asp:GridView ID="gridView" OnPageIndexChanging="gridView_PageIndexChanging" OnSorting="gridView_Sorting" runat="server" /> private string ConvertSortDirectionToSql(SortDirection sortDireciton) { string newSortDirection = String.Empty; switch (sortDirection) { case SortDirection.Ascending: newSortDirection = "ASC"; break; case SortDirection.Descending: newSortDirection = "DESC"; break; } return newSortDirection } protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e) { gridView.PageIndex = e.NewPageIndex; gridView.DataBind(); } protected void gridView_Sorting(object sender, GridViewSortEventArgs e) { DataTable dataTable = gridView.DataSource as DataTable; if (dataTable != null) { DataView dataView = new DataView(dataTable); dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection); gridView.DataSource = dataView; gridView.DataBind(); } }
Pop-up a Confirmation box before Deleting a row in GridView
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDel" runat="server" Text="Delete"
CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete the record?');" />
</ItemTemplate>
</asp:TemplateField>
Subscribe to:
Posts (Atom)