Data Grid
I am developing an application in ASP.NET with C# as the scripting language.I use a Datagrid.What i am planning is once the ADD link in the datagrid is clicked the datagrid should add a new row which contains text boxes.Once the the SAVE button is clicked it should save the values stored in textboxes to the table database.My problem is my datagrid is not showing on the screen.datarid shows on the screen only if it is bound with some data in the table in database.If the table in datatbase is empty datagrid wont show up.Also i need to display one row of the datagrid by default.And when i click the button it should insert a new row.I am attaching the code below
<%@ Page language="c#" Codebehind="NewUser.aspx.cs" AutoEventWireup="false" Inherits="TelecomGen.NewUser" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>NewUser</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
</P>
<DIV style="DISPLAY: inline; FONT-WEIGHT: bold; Z-INDEX: 101; LEFT: 112px; TEXT-TRANSFORM: capitalize; WIDTH: 159px; POSITION: absolute; TOP: 64px; HEIGHT: 40px; TEXT-DECORATION: underline"
align="center" ms_positioning="FlowLayout">Sign
Up</DIV>
<P></P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<TABLE id="Table1" style="Z-INDEX: 102; LEFT: 112px; WIDTH: 448px; POSITION: absolute; TOP: 128px; HEIGHT: 375px"
cellSpacing="4" cellPadding="5" width="448" border="0">
<TR>
<TD style="WIDTH: 134px; HEIGHT: 31px"><STRONG>First Name</STRONG></TD>
<TD style="HEIGHT: 31px"><INPUT id="txtFirstName" type="text"><STRONG></STRONG></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 34px"><STRONG>Middle Name</STRONG></TD>
<TD style="HEIGHT: 34px"><INPUT id="txtMidName" type="text"><STRONG></STRONG></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 31px"><STRONG>Last Name</STRONG></TD>
<TD style="HEIGHT: 31px"><INPUT id="txtLastName" type="text"><STRONG></STRONG></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 33px"><STRONG>EMail Id</STRONG></TD>
<TD style="HEIGHT: 33px"><INPUT id="txtEMail" type="text"><STRONG></STRONG></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 33px"><STRONG>Confirm EmailId</STRONG></TD>
<TD style="HEIGHT: 33px"><INPUT id="txtConfirmEMail" type="text"><STRONG></STRONG></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 35px"><STRONG>Password</STRONG></TD>
<TD style="HEIGHT: 35px"><INPUT id="txtPassword" style="WIDTH: 152px; HEIGHT: 22px" type="password" size="20"><STRONG></STRONG></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 29px"><STRONG>Password Confirm</STRONG></TD>
<TD style="HEIGHT: 29px"><INPUT id="txtPasswordConfirm" style="WIDTH: 152px; HEIGHT: 22px" type="password" size="20"></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 29px"><STRONG>Security Question</STRONG></TD>
<TD style="HEIGHT: 29px">
<asp:DropDownList id="DropDownList1" runat="server" Width="208px">
<asp:ListItem Value="[Select a Question To Answer]" Selected="True">[Select a Question To Answer]</asp:ListItem>
<asp:ListItem Value="What is your birth star">What is your birth star</asp:ListItem>
<asp:ListItem Value="what is your pets name">what is your pets name</asp:ListItem>
<asp:ListItem Value="what is your favorite past time">what is your favorite past time</asp:ListItem>
</asp:DropDownList></TD>
</TR>
<TR>
<TD style="WIDTH: 134px; HEIGHT: 29px"><STRONG>Your Answer</STRONG></TD>
<TD style="HEIGHT: 29px">
<asp:TextBox id="txtAnswer" runat="server"></asp:TextBox></TD>
</TR>
</TABLE>
<P>
</P>
<P> </P>
<P>   ;   ;
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button> &nb sp;   ; & nbsp; &nb sp;   ;
<TABLE id="Table2" style="WIDTH: 650px; HEIGHT: 158px" cellSpacing="1" cellPadding="1"
width="650" border="0">
<TR>
<TD>
<asp:DataGrid id="DataGrid1" runat="server" Width="640px" Height="202px" AutoGenerateColumns="False">
<ItemStyle BackColor="#FFFFC0"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#C00000" BackColor="#FFFFC0"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="From">
<ItemTemplate>
<asp:TextBox ID="fromText" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "FromCompany") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="To">
<ItemTemplate>
<asp:TextBox ID="toText" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ToCompany") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Year">
<ItemTemplate>
<asp:TextBox ID="txtYear" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "GenYear") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Type">
<ItemTemplate>
<asp:TextBox ID="txtType" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Type") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Add" CommandName="Add"></asp:ButtonColumn>
<asp:ButtonColumn Text="Remove" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#993366" BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid></TD>
</TD></TR>
</TABLE>
  ;</P>
</form>
&n bsp; &nbs p;
</body>
</HTML>
C# file
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace TelecomGen
{
/// <summary>
/// Summary description for NewUser.
/// </summary>
public class NewUser : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtAnswer;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!Page.IsPostBack)
{
BindGrid();
}
}
//BINDING WITH DATA GRID
private void BindGrid()
{
DataSet ds;
if(Session["data"]==null)
{
CreateDataSet();
}
ds=(DataSet)Session["data"];
DataGrid1.DataSource=ds;
DataGrid1.DataMember="Geneology";
DataGrid1.DataBind();
}
//CREATE DATASET FUNCTION
private void CreateDataSet()
{
DataSet ds=new DataSet();
SqlConnection cnn=new SqlConnection();
cnn.ConnectionString=System.Configuration.Configur ationSettings.AppSettings["dbConnection"];
SqlDataAdapter da=new SqlDataAdapter("select * from Geneology where 1=2",cnn);
da.Fill(ds,"Geneology");
}
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Add")
{
DataSet ds=(DataSet)Session["data"];
DataRow row=ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(row);
BindGrid();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedInd exChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
{
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
}
}
}
|