refreshing datagrid

D

dada

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(int.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)
{


}

}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top