Archive

Archive for December, 2006

hOW TO CREATE A rOW IN gRIDVIEW WITHOUT USING DATASOURCES

December 20, 2006 Leave a comment

 

protected void Page_Load(object sender, EventArgs e)

{

for (int i = 0; i < GridView2.Rows.Count; i ++)

{

if (IsPostBack)

{

DropDownList dlll = (DropDownList)GridView2.Rows[i].Cells[1].Controls[1];

string s1123 = dlll.SelectedItem.ToString();

if ((s1123.Equals("IsBlank")) || (s1123.Equals("IsEmpty")))

{

GridView2.Rows[i].Cells[2].Controls[1].Visible =

false;

GridView2.Rows[i].Cells[2].Controls[2].Visible =

false;

}

else

{

if ((s1123.Equals("From")))

{

TextBox tt = (TextBox)GridView2.Rows[i].Cells[2].FindControl("TextBox2");

tt.Visible =

true;

GridView2.Rows[i].Cells[2].Controls[1].Visible =

true;

GridView2.Rows[i].Cells[2].Controls[1].Visible = true;

}

else

{

GridView2.Rows[i].Cells[2].Controls[1].Visible =

true;

}

}

}

 

}

}

 

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)

{

}

protected void GridView2_SelectedIndexChanged1(object sender, EventArgs e)

{

}

protected void Button1_Click1(object sender, EventArgs e)

{

GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);

DropDownList DropDownListcolumn = new DropDownList();

DropDownListcolumn.DataSource = createDataset();

DropDownListcolumn.DataTextField = "Band_Name";

DropDownListcolumn.DataValueField=

"Band_Name";

DropDownListcolumn.DataBind();

 

 

DropDownList DropDownListOperator = new DropDownList();

DropDownListOperator.DataSource = createArray();

DropDownListOperator.DataBind();

TextBox textvalue1 = new TextBox();

TextBox textvalue2 = new TextBox();

textvalue1.Width=100;

textvalue1.Height=18;

 

DropDownList condition=new DropDownList();

condition.DataSource=createCondition();

condition.DataBind();

TableCell cell1 = new TableCell();

TableCell cell2 = new TableCell();

TableCell cell3 = new TableCell();

TableCell cell4 = new TableCell();

cell1.Controls.Add(DropDownListcolumn);

cell2.Controls.Add(DropDownListOperator);

cell3.Controls.Add(textvalue1);

cell4.Controls.Add(condition);

 

 

row.Cells.Add(cell1);

row.Cells.Add(cell2);

row.Cells.Add(cell3);

row.Cells.Add(cell4);

 

GridView2.Controls[0].Controls.AddAt(GridView2.Rows.Count + 1, row);

}

 

 

 

 

private DataSet createDataset()

{

string dbconnection = WebConfigurationManager.ConnectionStrings["OasisTempConnectionString4"].ConnectionString;

SqlConnection con = new SqlConnection(dbconnection);

 

 

con.Open();

 

SqlDataAdapter da = new SqlDataAdapter("SELECT DISTINCT [Band_Name] FROM [Dim_Band]", con);

DataSet ds = new DataSet();

da.Fill(ds);

return ds;

 

}

 

private ArrayList createArray()

{

ArrayList list = new ArrayList();

list.Add("IsBlank");

list.Add("IsEmpty");

list.Add(

"GreaterThan");

list.Add(

"LessThan");

list.Add(

"Equals");

list.Add(

"NotEquals");

return list;

}

private ArrayList createCondition()

{

ArrayList list = new ArrayList();

list.Add(

"AND");

list.Add(

"OR");

return list;

 

 

 

 

}

 

 

protected void RowCreated(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowState == DataControlRowState.Normal)

{

e.Row.Attributes.Add("onMouseOver", "this.style.background=’RoyalBlue’");

e.Row.Attributes.Add(

"onMouseOut", "this.style.background=’CornflowerBlue’");

}

else

{

e.Row.Attributes.Add(

"onMouseOver", "this.style.background=’RoyalBlue’");

e.Row.Attributes.Add(

"onMouseOut", "this.style.background=’InactiveCaptionText’");

}

//// e.Row.Attributes.Add("onClick", "alert(‘Selected date item index: " + e.Row.DataItemIndex.ToString() + "’)");

//e.Row.Attributes.Add("onMouseOver", "SetNewColor(this);");

//e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);");

}

 

 

 

protected void Button2_Click(object sender, EventArgs e)

{

foreach (GridViewRow gr in GridView2.Rows)

{

int i = 0;

 

for (int j = 0; j < gr.Controls.Count; j++)

{

string typeComponent = gr.Cells[i].Controls[1].GetType().ToString();

 

switch (typeComponent)

{

case "System.Web.UI.WebControls.DropDownList": String selected = "DropDownlist" + (j + 1);

DropDownList dll = (DropDownList)gr.Cells[i].Controls[1].FindControl(selected);

if (dll != null)

{

if (dll.SelectedValue != "")

{

TextArea1.Value += dll.SelectedValue;

}

else

{

i = -1;

}

}

break;

case "System.Web.UI.WebControls.TextBox":

string txtselected = "TextBox" + (j + 1);

TextBox txt = (TextBox)gr.Cells[i].Controls[1].FindControl(txtselected);

TextArea1.Value += txt.Text;

break;

 

 

}

TextArea1.Value += " ";

i++;

if (i == 0)

break;

}

}

 

 

}

 

 

 

HTML View OF Design

 

<%

@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml&quot; >

<

head runat="server">

<title>Untitled Page</title>

<meta http-equiv="Page-Exit"

content="progid:DXImageTransform.Microsoft.Fade(duration=2)" />

<script type="text/javascript">

 

 

// var oldColor;

//

// function SetNewColor(source)

// {

// oldColor = source.style.backgroundColor;

// source.style.backgroundColor = ‘RoyalBlue’;

// }

//

// function SetOldColor(source)

// {

// source.style.backgroundColor = oldColor;

// }

function displayMenu()

{

contextMenu.style.leftPos+=10;

contextMenu.style.posLeft=

event.clientX;

contextMenu.style.posTop=

event.clientY;

contextMenu.style.display=

"";

contextMenu.setCapture();

}

function clickMenu()

{

contextMenu.releaseCapture();

contextMenu.style.display=

"none";

}

function switchMenu() {

el=

event.srcElement;

if (el.className=="menuItem")

{

el.className=

"highlightItem";

}

else if (el.className=="highlightItem")

{

el.className=

"menuItem";

}

}

 

 

 

</

script>

<

style type="text/css">

.menuItem

{

font-family

:Tahoma, Monospace;

font-size:8pt;

width:10%;

z-index:200;

background-Color:#A1BBE4;

padding-top: 2px; padding-bottom: 2px;

margin-bottom:0;

margin-top:0;

cursor: default;

z-index:200;

}

.menuItemDefault

{

font-family

:Tahoma, Monospace;

font-size

:8pt;

font-weight:bold;

width:10%;

background-Color:RoyalBlue;

color:black;

padding-top: 2px;

padding-bottom: 2px;

cursor: default;

z-index:200;

}

.highlightItem {

font-family:Tahoma, Monospace;

font-size:8pt;

width:10%;

font-weight:Bold;

background-Color:RoyalBlue;

color:white;

padding-top: 2px;

padding-bottom: 2px;

cursor: default;

z-index:200;

}

 

 

 

</

style>

</

head>

<body>

<form id="form1" runat="server">

<div>

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />

<fieldset style=" width:100% ;height: 250px; border-right: #bcdaff 1px solid; border-top: #bcdaff 1px solid;

border-left: #bcdaff 1px solid; border-bottom: #bcdaff 1px solid;">

<legend style="font-weight: bold; font-size: 11px; color: #0066cc; font-family: Tahoma, Monospace;">

Where Clause

</legend>&nbsp; &nbsp;&nbsp;

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<table cellpadding="0" cellspacing="0">

<tr>

<td style=" height: 250px; width: 450px;">

<table border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse;position:relative;width:450px; top: 0px; height: 30px;">

<

tr>

<td style="width:90px;background-color:RoyalBlue;font-size:smaller;font-family:Tahoma;font-style:normal; font-weight: bold; color: #ffffff; height: 18px;" align="right">

Columns

</td>

<td style="width:90px;background-color:RoyalBlue;font-size:smaller;font-family:Tahoma;font-style:normal; font-weight: bold; color: #ffffff; height: 18px;" align="center">

Operators

</td>

<td align="center" style="width:100px;font-weight: bold; font-size: smaller; color: #ffffff; font-family: Tahoma, Monospace; background-color: royalblue; height: 18px;">

Value

</td>

<td align="right" style="width:90px;font-weight: bold; font-size: smaller; color: #ffffff; font-family: Tahoma, Monospace; background-color: royalblue; height: 18px;">

Condition

</td>

</

tr>

</

table>

<div style="vertical-align: top; height:198px; overflow:auto; position: static;z-index:-100" onContextMenu="displayMenu(); return false">

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

BorderColor="#E7E7FF" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" Height="193px" ShowHeader="False" Style="position: static;z-index:-10"

Width="435px" OnSelectedIndexChanged="GridView2_SelectedIndexChanged1" ShowFooter="True" OnRowCreated="RowCreated" >

<FooterStyle BackColor="RoyalBlue" ForeColor="#4A3C8C" BorderColor="Gainsboro" BorderStyle="Solid" BorderWidth="1px" />

<Columns>

<asp:TemplateField >

<ItemTemplate>

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"

DataTextField="Band_Name" DataValueField="Band_Name" Style="position: static;z-index:-100">

</asp:DropDownList>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Style="position: static;z-index:-100">

<asp:ListItem Selected="True">IsBlank</asp:ListItem>

<asp:ListItem>IsEmpty</asp:ListItem>

<asp:ListItem>GreaterThan</asp:ListItem>

<asp:ListItem>LessThan</asp:ListItem>

<asp:ListItem>Equals</asp:ListItem>

<asp:ListItem>NotEquals</asp:ListItem>

</asp:DropDownList>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Middle" ControlStyle-BorderStyle="None">

<ItemTemplate>

<asp:TextBox ID="TextBox3" runat="server" Height="18px" Style="position: static;z-index:-100"

Width="100px"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server" Height="18px" Style="position: static;z-index:-100"

Visible="False" Width="100px"></asp:TextBox>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemTemplate>

<asp:DropDownList ID="DropDownList4" runat="server" Style="position: static;z-index:-100">

<asp:ListItem Selected="True">AND</asp:ListItem>

<asp:ListItem>OR</asp:ListItem>

</asp:DropDownList>

</ItemTemplate>

<FooterTemplate>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click1" Style="position: static;z-index:-100"

Text="ADD" />

</FooterTemplate>

</asp:TemplateField>

</Columns>

<RowStyle BackColor="InactiveCaptionText" ForeColor="#4A3C8C" />

<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />

<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />

<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />

<AlternatingRowStyle BackColor="CornflowerBlue" BorderColor="Cyan" BorderStyle="Solid"

BorderWidth="1px" Font-Bold="True" Font-Names="Tahoma" Font-Size="Small" ForeColor="Bisque" />

</asp:GridView>

</div>

</td>

<td style="width: 450px; height: 250px" align="left" valign="middle">

<textarea id="TextArea1" style="width: 422px; position: static; height: 222px" runat="server" cols="20" rows="10"></textarea>&nbsp; &nbsp;

</td>

</tr>

</table>

</ContentTemplate>

</asp:UpdatePanel>

&nbsp; &nbsp;&nbsp;

<!–Context Menu —>

<

div id="contextMenu" onclick="clickMenu()" onmouseover="switchMenu()" onmouseout="switchMenu()" style="position:absolute; display:none; width:130; background-Color:InactiveCaption; border: 2px outset; z-index:300">

<div class="menuItem" style="font-weight:bold;" id="menuOpen">Add</div>

<div class="menuItem" id="menuEdit">Remove</div>

<div class="menuItem" id="menuExpand">Expand</div>

<div class="menuItem" id="menuCollapse">Collapse</div>

</div>

<!–End Of Context Menu —>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OasisTempConnectionString4 %>"

SelectCommand="SELECT DISTINCT [Band_Name] FROM [Dim_Band]"></asp:SqlDataSource>

<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Style="position: static"

Text="Priview" /></fieldset>

</div>

</form>

</

body>

</

html>

 

This page wrk for creating a new Row in gridview and context menu for gridview …I feel it is simple to understand …

Categories: ASP.NET
%d bloggers like this: