Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > refreshing datagrid

Reply
Thread Tools

refreshing datagrid

 
 
dada
Guest
Posts: n/a
 
      02-13-2004
Hi!
I datagrid (dataset) that I fill from webservice. When I add new row
to dataset, it`s ID it is writter on SQL server ok, but in datagrid
it gets last id in datagrid +1, untill I take edit and datagrid
refreshes from server and gets proper ID. For example, if I add new
record that has id 2 on server and delite it (now next row has to have
id 3), and again add new record in datagrid it shows id=2, but on
server is id=3 (correct).
So, I am asking you how to refresh my datagrid after adding new record
in the sime step.

my code html and aspx.cs is bellow.

Thank you,
Davorka

<body>
<form id="Institucije" method="post" runat="server">
<h3 style="COLOR: darkslateblue; FONT-FAMILY: 'Times New Roman'"
align="center">Ažuriranje
institucija</h3>
<asp:table id="Table1" Runat="server" Width="569px" Height="51px"
BorderColor="DarkSlateBlue" BorderWidth="1px" BorderStyle="Double"
HorizontalAlign="Center">
<asp:TableRow BorderColor="DarkSlateBlue" BorderStyle="None"
ID="Labele" BackColor="#CCCCFF" Font-Bold="True">
<asp:TableCell Width="120px" HorizontalAlign="Center"
BorderColor="DarkSlateBlue">
<asp:Label runat="server" ID="lblDodajNovi">Dodaj
novi</asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label runat="server" ID="lblNaziv">Naziv</asp:Label>
</asp:TableCell>
<asp:TableCell>
<asp:Label runat="server" ID="lblAdresa">Adresa</asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="NapraviNoviRed" BackColor="DarkSlateBlue">
<asp:TableCell ColumnSpan="3">
<asp:Button runat="server" Width="80px" ID="Create"
Text="Kreiraj" OnClick="Create_Click"></asp:Button>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow BorderWidth="1px" BorderColor="DarkSlateBlue"
BorderStyle="Solid" ID="DodajNoviRed" BackColor="DarkSlateBlue"
Visible="False">
<asp:TableCell>
<asp:Button runat="server" Width="75px" ID="Dodaj" Height="25px"
Text="OK" OnClick="Dodaj_Click"></asp:Button>
<asp:Button runat="server" Width="75px" ID="Odustani"
Height="25px" Text="Odustani" OnClick="Odustani_Click"></asp:Button>
</asp:TableCell>
<asp:TableCell VerticalAlign="Top" TabIndex="1">
<asp:TextBox runat="server" Width="250px"
ID="txNaziv"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" Display="Dynamic"
ControlToValidate="txNaziv" ID="ProveriNaziv"> <br> *Naziv je
obavezan!
</asp:RequiredFieldValidator>
</asp:TableCell>
<asp:TableCell VerticalAlign="Top" TabIndex="2">
<asp:TextBox runat="server" Width="250px"
ID="txAdresa"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:table><br>
<asp:datagrid id=DataGrid1 runat="server" Width="568px"
DataSource="<%# dsDepartmani %>" DataKeyField="ID institucije"
DataMember="Institucije" AutoGenerateColumns="False"
HorizontalAlign="Center" PageSize="15" BorderColor="DarkSlateBlue">
<ItemStyle VerticalAlign="Middle"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center"
VerticalAlign="Middle" BackColor="#CCCCFF"></HeaderStyle>
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Update" CancelText="Cancel" EditText="Edit">
<HeaderStyle Width="100pt"></HeaderStyle>
</asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete">
<HeaderStyle Width="100pt"></HeaderStyle>
</asp:ButtonColumn>
<asp:TemplateColumn HeaderText="ID institucije">
<HeaderStyle Width="250pt"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.ID institucije") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label id=Label2 runat="server" Width="92px" Text='<%#
DataBinder.Eval(Container, "DataItem.ID institucije") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Naziv">
<HeaderStyle Width="650pt"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Naziv") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Naziv") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Adresa">
<HeaderStyle Width="650pt"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.Adresa") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.Adresa") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid><br>
<br>
</form>
<script runat="server">
protected void Create_Click(object sender, EventArgs e)
{
ShowAddNewControls(true);
DataGrid1.EditItemIndex=-1;
DataGrid1.DataBind();
}

protected void ShowAddNewControls(bool ShowControls)
{txNaziv.Text="";
txAdresa.Text="";
DodajNoviRed.Visible=ShowControls;
NapraviNoviRed.Visible=!ShowControls;
txNaziv.Visible=ShowControls;
txAdresa.Visible=ShowControls;

}

private void Dodaj_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
WebRaspored2.localhost.DsDepartmani.InstitucijeRow instRow=
dsDepartmani.Institucije.NewInstitucijeRow();

instRow.Naziv =txNaziv.Text;
instRow.Adresa =txAdresa.Text;

dsDepartmani.Institucije.AddInstitucijeRow(instRow );
WebRaspored2.localhost.RasporedService s= new
WebRaspored2.localhost.RasporedService();
s.WebSnimi(dsDepartmani);
dsDepartmani.AcceptChanges();
DataGrid1.DataBind();
ShowAddNewControls(false);
}
}

protected void Odustani_Click(object sender, EventArgs e)
{
ShowAddNewControls(false);
}
</script>
</body>
</HTML>


using WebRaspored2.localhost;


namespace WebRaspored2
{
/// <summary>
/// Summary description for Institucije.
/// </summary>
public class Institucije : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox tbNaziv;
protected System.Web.UI.WebControls.TextBox tbAdresa;
protected System.Web.UI.WebControls.Button btnDodaj;
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.RequiredFieldValidator
RequiredFieldValidator1;
protected DsDepartmani dsDepartmani;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
localhost.RasporedService s= new localhost.RasporedService();
dsDepartmani=s.WebPopuni("Institucije");
DataGrid1.DataSource=dsDepartmani;
DataGrid1.DataMember="Institucije";

if(!IsPostBack)
DataGrid1.DataBind();

}

*Web Form Designer generated code*


private void DataGrid1_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();


}

private void DataGrid1_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//ShowAddNewControls(false);
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{ //ShowAddNewControls(false);
localhost.RasporedService s= new localhost.RasporedService();

string naz, adr;
string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
TextBox tb;
tb=(TextBox)(e.Item.Cells[3].Controls[1]);
naz = tb.Text;
tb=(TextBox)(e.Item.Cells[4].Controls[1]);
adr=tb.Text;
DsDepartmani.InstitucijeRow rd;
rd=dsDepartmani.Institucije.FindByID_institucije(i nt.Parse(key));
if (rd!=null)
{
rd.Naziv=naz;
rd.Adresa=adr;

// Calls a SQL statement to update the database from the dataset
//sqlDataAdapter1.Update(dsCategories1);
//localhost.RasporedService s= new localhost.RasporedService();

s.WebSnimi(dsDepartmani);
dsDepartmani.AcceptChanges();
}


// Takes the DataGrid row out of editing mode
DataGrid1.EditItemIndex = -1;

// Refreshes the grid
DataGrid1.DataBind();
}

private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
try
{
int rowToDelete = e.Item.ItemIndex;
// Add code to delete row from data source.
dsDepartmani.Institucije[rowToDelete].Delete();
localhost.RasporedService s= new localhost.RasporedService();
s.WebSnimi(dsDepartmani);
dsDepartmani.AcceptChanges();
DataGrid1.DataBind();
}
catch (InvalidCastException ex)
{


}

}


}
 
Reply With Quote
 
 
 
 
dada
Guest
Posts: n/a
 
      03-03-2004
I still haven`t solved the problem, somebody help please.
Dada
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Self-refreshing and non-self-refreshing controls Harlan Messinger ASP .Net 1 08-04-2006 01:13 PM
Self-refreshing and non-self-refreshing controls Harlan Messinger ASP .Net Web Controls 0 08-03-2006 12:29 PM
Refreshing the DataGrid in a webform in the IFrame Rod ASP .Net 4 08-18-2004 09:05 PM
Refreshing a DataGrid that is bound to a Collection of Business Objects =?Utf-8?B?S2VsbHkgUw==?= ASP .Net 1 05-19-2004 08:21 PM
Refreshing DataGrid in page_load A.M ASP .Net 7 11-04-2003 02:27 PM



Advertisments