Google Brother Up
2009/03/21
A different way to insert Data into a Gridview from array
string[] strs = strProjListFinal.Split(';');
ddlProjectName.Items.Clear();
foreach (string s in strs)
{
ddlProjectName.Items.Add(s.Trim());
}
ddlProjectName.Items.Insert(0, new ListItem("All Projects", "-1"));
236 unique visitors
2008/07/08
Export Gridview to Pdf
Soution: -Check the column name of your gridview. Whether they match with the ones you send to the data table or not. If they match. Then this error will not be shown.
Export Gridview to Pdf: -
1) First of all add reference the dll file called itextsharp.dll to your solution under References.
It can be obtained from: -
2) Then write the code below: -
using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf;
protected void btnExportToPdf_Click(object sender, EventArgs e) { ExportToPDF(); }
private void ExportToPDF() { Document document = new Document(PageSize.A4, 0, 0, 50, 50); System.IO.MemoryStream msReport = new System.IO.MemoryStream();
try { // creation of the different writers PdfWriter writer = PdfWriter.GetInstance(document, msReport);
// we add some meta information to the document document.AddAuthor("Vaichatt"); document.AddSubject("Export to PDF");
document.Open();
iTextSharp.text.Table datatable = new iTextSharp.text.Table(5);
datatable.Padding = 2; datatable.Spacing = 0;
//float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 }; float[] headerwidths = { 10, 50, 10, 15, 15 }; datatable.Widths = headerwidths;
// the first cell spans 7 columns // the first cell spans 5 columns Cell cell = new Cell(new Phrase("Manage Category Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD))); cell.HorizontalAlignment = Element.ALIGN_CENTER; cell.Leading = 30; //cell.Colspan = 7; cell.Colspan = 5; cell.Border = Rectangle.NO_BORDER; cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray); datatable.AddCell(cell);
// These cells span 2 rows datatable.DefaultCellBorderWidth = 1; datatable.DefaultHorizontalAlignment = 1; datatable.DefaultRowspan = 2; datatable.AddCell("Status"); datatable.AddCell(new Phrase("Category Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.NORMAL))); datatable.AddCell("Edit"); datatable.AddCell("Delete"); datatable.AddCell("Products");
// This cell spans the remaining 3 columns in 1 row //datatable.DefaultRowspan = 1; //datatable.DefaultColspan = 3; //datatable.AddCell("Just Put Anything");
// These cells span 1 row and 1 column //datatable.DefaultColspan = 1; //datatable.AddCell("Col 1"); //datatable.AddCell("Col 2"); //datatable.AddCell("Col 3");
datatable.DefaultCellBorderWidth = 1; datatable.DefaultRowspan = 1;
for (int i = 1; i < dgdManageCategory.Rows.Count; i++) { datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT; //datatable.AddCell(i.ToString()); //datatable.AddCell("This is my name."); //datatable.AddCell("I have a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long address."); //datatable.AddCell("0123456789");
//datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER; //datatable.AddCell("No"); //datatable.AddCell("Yes"); //datatable.AddCell("No");
CheckBox cbIsActive = (CheckBox)dgdManageCategory.Rows[i].FindControl("cbIsActive"); datatable.AddCell(Convert.ToString(cbIsActive.Checked ));
Label lblCategoryName = (Label)dgdManageCategory.Rows[i].FindControl("lblCategoryName"); datatable.AddCell(lblCategoryName.Text);
//Label lblCatName = (Label)dgdManageCategory.Rows[i].FindControl("lblCatName"); //datatable.AddCell(lblCatName.ToString());
Button btnEdit = (Button)dgdManageCategory.Rows[i].FindControl("lbnEdit"); datatable.AddCell(btnEdit.Text);
Button btnDelete = (Button)dgdManageCategory.Rows[i].FindControl("lbnDelete"); datatable.AddCell(btnDelete.Text);
Button btnProducts = (Button)dgdManageCategory.Rows[i].FindControl("lbnProducts"); datatable.AddCell(btnProducts.Text); }
document.Add(datatable); } catch (Exception e) { string s = e.Message; Console.Error.WriteLine(e.Message); }
// we close the document document.Close();
Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=Export.pdf"); Response.ContentType = "application/pdf"; Response.BinaryWrite(msReport.ToArray()); Response.End();
//http://july-code.blogspot.com/2008/06/export-gridview-to-pdf.html }
//Get Help From:-// http://july-code.blogspot.com/2008/06/export-gridview-to-pdf.html//http://www.pdftron.com/net/usermanual.html
42 unique visitors
2008/07/01
Import to Gridview from Excel Sheet (xlsx)
It should be made: -
OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" +
Server.MapPath("~/App_Data/Running.xlsx") + ";" + "Extended Properties=\"Excel 12.0;HDR=Yes\"");
Rest is same.
The code will run properly.
Now the challenge is to select the path of the file at runtime.
38 visitors
Import to Gridview from Excel Sheet (xls)
Step 1 – Create Excel worksheet
1) Open Microsoft Excel and create a new Worksheet.
2) The sample data is as follows: - (any sample data)
We will keep the default name for the Worksheet (Sheet1).
3) Let us name and save the excel file as Running.xls.
Note: You can download the Excel file along with the code files from the Downloads section at the end of this article.
Step 2 –Display Excel Data using GridView Control
1) Start Visual Studio 2005.
2) Select Create Website and choose the Template ASP.NET Web Site. We can choose the language as C#/VB. Set the name for the project as ExcelGV.
3) Add the existing Excel file (Running.xls) that we created to the App_data folder.
4) Add a new .aspx file. Choose language C#/VB and, depending on the language, we will set the appropriate names: ExcelGVCS.aspx for C# or ExcelGVVB for VB.NET.
4) Drag and drop the GridView control on the .aspx page.
5) We will add the following statement in code behind above the namespace section.
using System.Data.OleDb;
------------------------------
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("~/App_Data/Running.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
string SQLString = "SELECT * FROM [Sheet1$]";
OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
IDataReader DBReader = DBCommand.ExecuteReader();
GridView1.DataSource = DBReader;
GridView1.DataBind();
DBReader.Close();
DBConnection.Close();
}
-----------------------------------------------------------------------------------
38 visitors
2008/05/08
THIS DATASOURCE DOES NOT SUPPORT SERVER-SIDE DATA PAGING
2008/01/10
Another Typical GridView
(%--(asp:GridView ID="dgdManageCategory" runat="server" AutoGenerateColumns="false" OnRowCommand="dgdManageCategory_RowCommand" OnRowDeleting="dgdManageCategory_RowDeleting" OnRowEditing="dgdManageCategory_RowEditing")
(Columns)
(asp:TemplateField HeaderText="CategoryID" Visible="false")
(ItemTemplate)
(asp:Label ID="lblCatID" runat="server" Text='(%# Eval("CategoryID")%)' /)
(/ItemTemplate)
(/asp:TemplateField)
(asp:TemplateField HeaderText="Status")
(ItemTemplate)
(asp:CheckBox ID="cbIsActive" runat="server" Checked='(%# Convert.ToBoolean(Eval("IsActive"))%)' /)
(/ItemTemplate)
(/asp:TemplateField)
(asp:BoundField DataField="CategoryName" HeaderText="Category Name" ConvertEmptyStringToNull="true" /)
(asp:TemplateField HeaderText="Edit")
(ItemTemplate)
(asp:Button ID="lbnEdit" runat="server" CommandArgument='(%# Eval("CategoryID")%)' CommandName="Edit" Text="Edit" /)
(/ItemTemplate)
(/asp:TemplateField)
(asp:TemplateField HeaderText="Delete")
(ItemTemplate)
(asp:Button ID="lbnDelete" runat="server" CommandArgument='(%# Eval("CategoryID")%)' CommandName="Delete" Text="Delete"
OnClientClick="JavaScript:return confirm('Are you sure you want to delete ');"/)
(/ItemTemplate)
(/asp:TemplateField)
(asp:TemplateField HeaderText="Products")
(ItemTemplate)
(asp:Button ID="lbnProducts" runat="server" CommandArgument='(%# Eval("CategoryID")%)' CommandName="Products" Text="Products" /)
(/ItemTemplate)
(/asp:TemplateField)
(/Columns)
(/asp:GridView)--%)
A Typical GridView
(asp:GridView ID="dgdManageUsers" runat="server" AutoGenerateColumns="False"
AllowPaging="true" PagerStyle-HorizontalAlign="Right" PagerSettings-Mode="Numeric" PagerSettings-PageButtonCount="3"
OnPageIndexChanging="dgdManageUsers_PageIndexChanging" EmptyDataText="No records found" OnRowDeleting="dgdManageUsers_RowDeleting"
OnRowCommand="dgdManageUsers_RowCommand" )
(Columns)
(asp:TemplateField HeaderText="CategoryID" Visible="False")
(ItemTemplate)
(asp:Label ID="lblUserID" runat="server" Text='(%# Eval("UserId")%)' /)
(/ItemTemplate)
(/asp:TemplateField)
(asp:BoundField HeaderText="Full Name" DataField="FullName" /)
(asp:BoundField HeaderText="Email" DataField="Email" /)
(asp:BoundField HeaderText="Registration Date" DataField="RegistrationDate" /)
(asp:TemplateField HeaderText="Edit")
(ItemTemplate)
(asp:Button runat="server" ID="btnEdit" CommandName="Edit" CommandArgument='(%# Eval("UserId")%)' Text="Edit"/)
(/ItemTemplate)
(/asp:TemplateField)
(asp:TemplateField HeaderText="Delete")
(ItemTemplate)
(asp:Button runat="server" ID="btnDelete" CommandName="Delete" CommandArgument='(%# Eval("UserId")%)' Text="Delete"
OnClientClick="JavaScript:return confirm('Are you sure you want to delete ');" /)
(/ItemTemplate)
(/asp:TemplateField)
(asp:TemplateField)
(ItemTemplate)
(asp:TextBox runat="server" ID="txtEditBox" Visible="False" Text='(%# DataBinder.Eval(Container.DataItem,"Email") %)' /)
(/ItemTemplate)
(/asp:TemplateField)
(/Columns)
(PagerSettings PageButtonCount="3" /)
(PagerStyle HorizontalAlign="Right" /)
(/asp:GridView)
2008/01/07
The GridView 'gdABCD' fired event PageIndexChanging which wasn't handled.
DataGridPageChangedEventArgs to GridViewPageEventArgs.
2) If you set AllowPaging="true" or AllowSorting="true" on a GridView control without using a DataSourceControl DataSource (i.e. SqlDataSource, ObjectDataSource), you will run into the following errors: -
When changing the page on the GridView control: -
The GridView 'GridViewID' fired event PageIndexChanging which wasn't handled.
When clicking a column name to sort the column on the GridView control: -
The GridView 'GridViewID' fired event Sorting which wasn't handled.
As a result of not setting the DataSourceID property of the GridView to a DataSourceControl DataSource, you have to add event handlers for sorting and paging.
GridView PageIndex Code Behind
{
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}
Paging concepts in GridView
The DataGrid control requires additional coding for paging. The GridView control automatically supports paging by setting the PagerSettings property. The PagerSettings property supports four modes: Numeric ( default ), NextPrevious, NumericFirstLast, and NextPreviousFirstLast.
The Numeric mode displays numbered page links instead of "next/prev" links, and the NumericFirstLast option adds first and last page links. The GridView control's PagerStyle property can be used to set styles and the position of the pager.
Additionally, you can customize the pager buttons for the GridView control using a PagerTemplate.
Custom paging support in the GridView is supplied by the bound data source control as opposed to the AllowCustomPaging mechanism supplied by the DataGrid control.
2008/01/04
Get Row Index with Gridview Select Button
Inside that event, try something like this (with a label called 'Label1':
Dim row As GridViewRow = MyGridView.SelectedRow
Dim intRow as Integer=Row.RowIndex
label1.text=intRow.ToString
or
GridViewRow gvr = new GridViewRow();
gvr = dgdManageUsers.SelectedIndex;
int introw = gvr.RowIndex;
label1.text=intRow.ToString;