Friday, January 25, 2008

Binding GridView Controls By Database

<%@ Register Assembly="MagicAjax" Namespace="MagicAjax.UI.Controls" TagPrefix="ajax" %>



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"

BorderColor="White" BorderStyle="Ridge" BorderWidth="3px" CellPadding="3" CellSpacing="1" OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated"

GridLines="None" Style="position: static" AllowPaging="True" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnSorting="GridView1_Sorting">

<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />

<Columns>

<asp:TemplateField HeaderText="Bank ATM Locations">

<ItemTemplate>

<asp:Label ID="Label_Bank" runat="server" Font-Names="Arial" Font-Size="8pt" Style="position: static"

Text='<%# Eval("Branch_Name") %>' Visible="False">asp:Label><ajax:AjaxPanel ID="AjaxPanel1" runat="server">

<asp:LinkButton ID="LinkButton_Location" runat="server" Style="position: static" Text='<%# Eval("Branch_Name") %>'>asp:LinkButton>ajax:AjaxPanel>

<asp:Label ID="Label_Latitude" runat="server"

Style="position: static" Visible="False" Text='<%# Eval("LatiTude") %>'>asp:Label> <asp:Label ID="Label_Longitude"

runat="server" Style="position: static" Visible="False" Text='<%# Eval("Longitude") %>'>asp:Label>

ItemTemplate>

asp:TemplateField>

<asp:TemplateField HeaderText="Bank ATM Address">

<ItemStyle HorizontalAlign="Center" />

<HeaderStyle HorizontalAlign="Center" />

<ItemTemplate>

<asp:Label ID="Label_ROIfl" runat="server" Style="position: static" Text='<%# Eval("Address") %>'>asp:Label>

ItemTemplate>

asp:TemplateField>

Columns>

<RowStyle BackColor="#DEDFDE" Font-Size="8pt" ForeColor="Black" />

<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#C6C3C6" Font-Overline="False" ForeColor="Black" />

<HeaderStyle BackColor="#004040" Font-Bold="True" Font-Size="8pt" ForeColor="#E7E7FF"

Wrap="False" />

asp:GridView>

=========================================================

Now Add This Code in ".cs" file in some event ...

//Fill the DataList dynamically on user's choice of banks ...

private void FillGridView(string SelectedBankIds)

{

string str = "SELECT Bank_Name, Website FROM Bank_List WHERE (Bank_Id IN (" + SelectedBankIds + "))";

// DataSet dt = new DataSet();

DataTable dt = new DataTable();

SqlDataAdapter da = new SqlDataAdapter(str, sqlcon);

da.Fill(dt);

if (dt.Rows.Count == 0)

{

// Remove contraints so an empty row can be added...

dt.Constraints.Clear();

foreach (DataColumn dc in dt.Columns)

dc.AllowDBNull = true;

// Add a blank row to the dataset...

dt.Columns[0].AllowDBNull = true;

dt.Rows.Add(dt.NewRow());

// Bind the DataSet to the GridView...

GridView1.Visible = true;

GridView1.DataSource = dt;

GridView1.DataBind();

// Get the number of columns to know what the Column Span should be...

int columnCount = GridView1.Rows[0].Cells.Count;

// Call the clear method to clear out any controls that you use in the columns. I use a dropdown list in one of the column so this was necessary....

GridView1.Rows[0].Cells.Clear();

GridView1.Rows[0].Cells.Add(new TableCell());

GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;

GridView1.Rows[0].Cells[0].Text ="No Bank Selected!";

}

else

{

//Clear the databinding ...

GridView1.DataSource = null;

GridView1.DataBind();

//Rebind with the database ...

GridView1.DataSource = dt;

GridView1.DataBind();

}

}


-
ShriKrishna Bhardwaj , With ASP.Net, SQL Server, Javascript

No comments: