473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem when adding new datagrid item

Hello !

I've got weird problem when adding new datagrid item. Here is the situation:
my grid dgDeps is binded to DepartmentList arraylist, stored in Session
between round-trips..
To add new item I press buttton with ibNewDep_Click event.
After that the grid is in edit state and I can modify new item, it has some
text, set as default - "<nowy wydzia³>".
But - this is the weirdness - when I add some text following the default
value
e.g "<nowy wydzial>xyz", the default text is not shown in grid cell after
edit finishes. What I can see is "xyz" !!!!
When I edit the item once again, texbox shows "<nowy wydzial>xyz".
Seems like grid and its datasource are out of sync. They become sync'ed when
I cut off the default text and text entered during edition is the only
contents of that item.

Any suggestions will be appreciated.

I include whole my code, to present the whole situation.
HTML part:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LibraryDepartments.ascx.cs"
Inherits="interLAN.DNN.Modules.Library.LibraryDepa rtments"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<P>
<asp:ImageButton id="ibNewDep" runat="server" ImageUrl="~/images/add.gif"
ToolTip="Nowy wydzia³"></asp:ImageButton>
<asp:DataGrid id="dgDeps" runat="server" DataKeyField="ItemID"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Zakoñcz"
CancelText="Przerwij" EditText="Edytuj"></asp:EditCommandColumn>
<asp:BoundColumn DataField="Name" SortExpression="Name" HeaderText="Nazwa
wydzia³u"></asp:BoundColumn>
<asp:ButtonColumn Text="Kasuj" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid></P>
Code behind part:
namespace interLAN.DNN.Modules.Library
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class LibraryDepartments : UserControl
{
private const string Name_ColumnName="Name";
private const string Delete_ColumnName="Delete";
protected System.Web.UI.WebControls.ImageButton ibNewDep;
protected System.Web.UI.WebControls.DataGrid dgDeps;

private ArrayList DepartmentList
{
get
{
return (ArrayList)Session[ClientID+"_DepartmentList"];
}
set
{
Session[ClientID+"_DepartmentList"]=value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
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.ibNewDep.Click += new
System.Web.UI.ImageClickEventHandler(this.ibNewDep _Click);
this.dgDeps.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgDeps_CancelComm
and);
this.dgDeps.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgDeps_EditComman
d);
this.dgDeps.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgDeps_UpdateComm
and);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private InterLANLibraryDepartmentInfo FindListDepById(int depId, ArrayList
depList)
{
foreach (InterLANLibraryDepartmentInfo d in depList)
{
if (d.ItemID==depId)
return d;
}
return null;
}
private InterLANLibraryDepartmentInfo FindListDepById(int depId)
{
return FindListDepById(depId, this.DepartmentList);
}
private int SetNewTempItemId()
{
Random r = new Random(1);
return -r.Next(2,100000000);
}
private void ibNewDep_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
InterLANLibraryDepartmentInfo d = new InterLANLibraryDepartmentInfo();
d.ItemID=SetNewTempItemId();
d.Name="<nowy wydzia³>";
this.DepartmentList.Insert(0,d);
dgDeps.EditItemIndex=this.DepartmentList.IndexOf(d );
BindGrid();
}

private void dgDeps_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=e.Item.ItemIndex;
BindGrid();
}

private void dgDeps_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=-1;
BindGrid();
}

private void dgDeps_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int NameColumnNr = 1;
foreach (DataGridColumn c in dgDeps.Columns)
{
if ((c is BoundColumn) &&
((c as BoundColumn).DataField==Name_ColumnName))
{
NameColumnNr=dgDeps.Columns.IndexOf(c);
break;
}
}
string NameText = ((TextBox)e.Item.Cells[NameColumnNr].Controls[0]).Text;
int depId = (int)dgDeps.DataKeys[e.Item.ItemIndex];
// zmiana warto¶ci w li¶cie
InterLANLibraryDepartmentInfo d = this.FindListDepById(depId);
if (d!=null)
d.Name=NameText;

dgDeps.EditItemIndex=-1;
BindGrid();
}

private void BindGrid()
{
dgDeps.DataSource=this.DepartmentList;
dgDeps.DataBind();
}
public override void LoadData()
{
dgDeps.EditItemIndex=-1;
// bo inaczej grid pozostaje w trakcie edycji
// je¶li wywo³ano by³ tu¿ przed wywo³anie tej metody

this.DepartmentList =
(new InterLANLibraryDepartmentController()).GetAll();

this.BindGrid();
}
}
}

--
Tomek R.
Nov 19 '05 #1
2 1900
Hi,

The problem is with the brackets < > , because the grid is just a table in
ASP.NET, so if any string is surrounded by < > brackets, then your browser
will treat it as a HTML tag, eventhough it is wrong, nothing will be
displayed. So replace the brackets <> with someother characters, say for eg.
[]

/Thangam
"Tomek R." wrote:
Hello !

I've got weird problem when adding new datagrid item. Here is the situation:
my grid dgDeps is binded to DepartmentList arraylist, stored in Session
between round-trips..
To add new item I press buttton with ibNewDep_Click event.
After that the grid is in edit state and I can modify new item, it has some
text, set as default - "<nowy wydzia³>".
But - this is the weirdness - when I add some text following the default
value
e.g "<nowy wydzial>xyz", the default text is not shown in grid cell after
edit finishes. What I can see is "xyz" !!!!
When I edit the item once again, texbox shows "<nowy wydzial>xyz".
Seems like grid and its datasource are out of sync. They become sync'ed when
I cut off the default text and text entered during edition is the only
contents of that item.

Any suggestions will be appreciated.

I include whole my code, to present the whole situation.
HTML part:
<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="LibraryDepartments.ascx.cs"
Inherits="interLAN.DNN.Modules.Library.LibraryDepa rtments"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<P>
<asp:ImageButton id="ibNewDep" runat="server" ImageUrl="~/images/add.gif"
ToolTip="Nowy wydzia³"></asp:ImageButton>
<asp:DataGrid id="dgDeps" runat="server" DataKeyField="ItemID"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="Zakoñcz"
CancelText="Przerwij" EditText="Edytuj"></asp:EditCommandColumn>
<asp:BoundColumn DataField="Name" SortExpression="Name" HeaderText="Nazwa
wydzia³u"></asp:BoundColumn>
<asp:ButtonColumn Text="Kasuj" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:DataGrid></P>
Code behind part:
namespace interLAN.DNN.Modules.Library
{
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public class LibraryDepartments : UserControl
{
private const string Name_ColumnName="Name";
private const string Delete_ColumnName="Delete";
protected System.Web.UI.WebControls.ImageButton ibNewDep;
protected System.Web.UI.WebControls.DataGrid dgDeps;

private ArrayList DepartmentList
{
get
{
return (ArrayList)Session[ClientID+"_DepartmentList"];
}
set
{
Session[ClientID+"_DepartmentList"]=value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
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.ibNewDep.Click += new
System.Web.UI.ImageClickEventHandler(this.ibNewDep _Click);
this.dgDeps.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgDeps_CancelComm
and);
this.dgDeps.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgDeps_EditComman
d);
this.dgDeps.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgDeps_UpdateComm
and);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private InterLANLibraryDepartmentInfo FindListDepById(int depId, ArrayList
depList)
{
foreach (InterLANLibraryDepartmentInfo d in depList)
{
if (d.ItemID==depId)
return d;
}
return null;
}
private InterLANLibraryDepartmentInfo FindListDepById(int depId)
{
return FindListDepById(depId, this.DepartmentList);
}
private int SetNewTempItemId()
{
Random r = new Random(1);
return -r.Next(2,100000000);
}
private void ibNewDep_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
InterLANLibraryDepartmentInfo d = new InterLANLibraryDepartmentInfo();
d.ItemID=SetNewTempItemId();
d.Name="<nowy wydzia³>";
this.DepartmentList.Insert(0,d);
dgDeps.EditItemIndex=this.DepartmentList.IndexOf(d );
BindGrid();
}

private void dgDeps_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=e.Item.ItemIndex;
BindGrid();
}

private void dgDeps_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgDeps.EditItemIndex=-1;
BindGrid();
}

private void dgDeps_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int NameColumnNr = 1;
foreach (DataGridColumn c in dgDeps.Columns)
{
if ((c is BoundColumn) &&
((c as BoundColumn).DataField==Name_ColumnName))
{
NameColumnNr=dgDeps.Columns.IndexOf(c);
break;
}
}
string NameText = ((TextBox)e.Item.Cells[NameColumnNr].Controls[0]).Text;
int depId = (int)dgDeps.DataKeys[e.Item.ItemIndex];
// zmiana warto¶ci w li¶cie
InterLANLibraryDepartmentInfo d = this.FindListDepById(depId);
if (d!=null)
d.Name=NameText;

dgDeps.EditItemIndex=-1;
BindGrid();
}

private void BindGrid()
{
dgDeps.DataSource=this.DepartmentList;
dgDeps.DataBind();
}
public override void LoadData()
{
dgDeps.EditItemIndex=-1;
// bo inaczej grid pozostaje w trakcie edycji
// je¶li wywo³ano by³ tu¿ przed wywo³anie tej metody

this.DepartmentList =
(new InterLANLibraryDepartmentController()).GetAll();

this.BindGrid();
}
}
}

--
Tomek R.

Nov 19 '05 #2
Gold !

Thanks for effective help.
--
Tomek R.
Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
1
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I have a problem with capturing the changed value of a text box in a datagrid. The datagrid is populated based on a dataset and I am using template...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
0
by: Raj Anand | last post by:
I have a dataview and binding to a datagrid. On ItemBound I'm adding a checkbox dynamically for each rows. It works ok but my problem is on checkedChanged i have addded a eventHandler..it's not...
1
by: Mikkel Olsen | last post by:
Hi. I have a problem regarding to the DataGrid_ItemDataBound event. I cant run this event when my datagrid is inside a DataList. The ItemDataBound for DataList1 is working well, but it never...
7
by: Neo Geshel | last post by:
Greetings. I have a serious problem. I have multiple sets of tables, several of which are chained more than two tables deep. That is, I have a parent, a child, and a great-grandchild table. ...
7
by: Girish | last post by:
OK.. phew. Playing with data grids for the past few days has been fun and a huge learning experience.. My problem. I have a requirement to display a gird with a gird. Within the embedded grid,...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.