HOME | ABOUT US | EXAMPLE | PROJECTS | INTERVIEW | SOURCE CODE | FRAMEWORK | CONTACT | JOBS | WALKIN
oops questions
ado.netassemblyasp.net basicobject oriented programmingcom dcomcssremotingsql servervb.netweb servicesxml
 

Source Code - Coding Syntax

datagrid session 2 2
Question: Here in this article i have discuss where connection string is stored and how to use that connection string after that i have also shows how a DataAdapter is filled the dataset and how to bind datagrid with that dataset after that how to Bind DropDownList with DataSet After Fill with a specific column in the table and also bind ListBox with database table and also write code to import data of DataGrid into excel and also how to take a total of specific column in footer of the DataGrid and also explain DataList how it is bind with DataSet and also its template column and some overview of its template column and also how to get DataGrid data in a string to send datagrid with mail body?

Answer: First of All ConnectionString in Web.Config file

<configuration>
<appSettings>
<add key="ConnectionString" value="server=localhost;database=Northwind;uid=sa;password=secret;" />
</appSettings>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>

Here is code to get DataGrid data in string and also can send this with body of mail
StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
System.Web.UI.HtmlTextWriter htmlTW = new  HtmlTextWriter(SW);
DataGrid1.RenderControl(htmlTW);
string dataGridHTML = SB.ToString();
      

Here is code to Create A connection Using Web.Config
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

Here is code to Fill DataSet with Data Table
SqlDataAdapter cmd=new SqlDataAdapter("select * from table_name",conn);
DataSet ds=new DataSet();
cmd.fill(ds,"table_name");

DataGrid/Gridview


Now last step to bind with DataGrid/Gridview
DataGrid1.DataSource=ds;
DataGrid1.DataBind();

Now Binding DropDownList
DropDownList.DataSource=ds;
DropDownList.DataTextField=ds.Tables[“table_name”].Column[“column_name”].tostring();
DropDownList.DataBind();

Code to Bind ListBox ?
ListBox1.DataSource=ds;
ListBox1.DataTextField=ds.Tables[“table_name”].Column[“column_name”].tostring();
ListBox1.DataBind();

Code to Import Data in Excel from DataGrid ?
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(DataGrid1);
DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();


Total of a column in Footer of the DataGrid ?
public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[1].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[1].Text));
Total=Total + Convert.Todouble(e.item.cells[1].Text);
}
else if(e.Item.ItemType == ListItemType.Footer )
{
e.Item.Cells[0].Text="Total";
e.Item.Cells[1].Text = string.Format("{0:c}", Total);
}
}

DataList


DataList:-After Repeater control there another helpful data control is DataList control it have some advantage over Repeater that is why its comes in to existence.DataList have a simple syntax to bind data with datalist.
(1)We can also bind a DataList with Array ?
string[] dotnet =
{
"dotnetquestion",
"sqlquestions",
"remoting"
};
DataList1.DataSource = dotnet;
DataList1.DataBind();
(2)To bind DataList with dataset ?
DataList1.DataSource=Dataset;
DataList1.DataBind();
(3)How many Template in DataList Here I have explain with syntax ?
<html>
<body>
<form runat="server">
<asp:DataList id="DataList1" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<AlternatingItemTemplate>
……
</AlternatingItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
(4)How to Bind Data with column from HTML code ?
<ItemTemplate>
"<%#Container.DataItem("Item")%>" of
<%#Container.DataItem("Itemcode")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>
<AlternatingItemTemplate>
"<%#Container.DataItem("Item ")%>" of
<%#Container.DataItem("Itemcode")%> -
$<%#Container.DataItem("price")%>
</AlternatingItemTemplate>
ADO.NET | ASSEMBLY | JAVA SCRIPT | OOPS | COM-DCOM | CSS | REMOTING | SQL SERVER | VB.NET | WEB SERVICES | XML | C#
SITEMAP | SQL SERVER QUERY | RESOURCES | ONLINE BOOKS SHOP
©2006-2007, dotnetquestion.info. All Rights Reserved.
Best View 1024 x 768