How to export data in Datagrid on a webform to Microsoft Excel

Two techniques for exporting the data in the DataGrid: Using the Excel MIME Type (or Content Type) With server-side code, you can bind the DataGrid to your data and have the data open in Excel on a client computer. To do this, set the ContentType to application/vnd.ms-excel. After the client receives the new stream, the data appears in Excel as if the content was opened as a new page in the Web browser. Using Excel Automation With client-side code, you can extract the HTML from the DataGrid and then Automate Excel to display the HTML in a new workbook. With Excel Automation, the data always appears outside the browser in an Excel application window. One advantage to Automation is that you can programmatically control Excel if you want to modify the workbook after the data is exported. However, because Excel is not marked as safe for scripting, your clients must apply security settings in the Web browser that allow Automation. For more details refer How To Export Data in a DataGrid on an ASP . NET WebForm to Microsoft Excel

How can you use a ListBox control to display items with Price above a specific value in one color and the ones below that value in a different color

The ListBox Web server control prevents us from assigning the style property to each item in the ListBox. This bug is confirmed by Microsoft Knowledge Base Article – 309338 So, instead use a HTML ListBox with runat=server <SELECT id=’listbox1′ size=’14’ runat=’server’ > </SELECT> VB.NET Dim myconnection As SqlConnection Dim myda As SqlDataAdapter Dim ds As DataSet Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myconnection = New SqlConnection(‘Server=localhost;uid=sa;password=;database=northwind;’) myda = New SqlDataAdapter(‘Select * from Products ‘, myconnection) ds = New DataSet() myda.Fill(ds, ‘AllTables’) dim i as Integer For i = 0 To ds.Tables(0).Rows.Count – 1 listBox1.Items.Add(New ListItem(ds.Tables(0).Rows(i)(‘UnitPrice’), ds.Tables(0).Rows(i)(‘ProductID’))) If ds.Tables(0).Rows(i)(‘UnitPrice’) <= 25 Then listBox1.Items(i).Attributes.Add(‘style’, ‘color:red’) Else listBox1.Items(i).Attributes.Add(‘style’, ‘color:green’) End If Next End Sub C# SqlConnection mycn; SqlDataAdapter myda; DataSet ds; String strConn; private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { strConn=’Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind’; mycn = new SqlConnection(strConn); myda = new SqlDataAdapter (‘Select * FROM Products ‘, mycn); ds = new DataSet(); myda.Fill (ds,’Table’); for(int i = 0 ;i < ds.Tables[0].Rows.Count – 1;i++) { listBox1.Items.Add (new ListItem(ds.Tables[0].Rows[i][‘UnitPrice’].ToString(), ds.Tables[0].Rows[i][‘ProductID’].ToString())); if(Convert.ToDouble(ds.Tables[0].Rows[i][‘UnitPrice’].ToString()) <= 25 ) { listBox1.Items[i].Attributes.Add(‘style’, ‘color:red’); } else { listBox1.Items[i].Attributes.Add(‘style’, ‘color:green’); } } } }