Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Grid SelectedItemIndex

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
Hi, I have a datagrid and on update command I try to set its SelecyedItemIndex to whatever user chose and re-bind datagrid. The problem is that after the page loads, data grid does nto show up! What could be the problem? Here is some code:

public void Edit(object sender, DataGridCommandEventArgs e)
{
//highlight selected row
CategoryDataGrid.EditItemIndex = e.Item.ItemIndex;

CategoryDataGrid.DataSource = Catalog.Tables["Category"].DefaultView;
CategoryDataGrid.DataBind();
}

Here is datagrid:
<asp:datagrid id="CategoryDataGrid" OnEditCommand="Edit" OnDeleteCommand="Delete" OnCancelCommand="Cancel"
DataKeyField="CategoryID" AutoGenerateColumns="False" Runat="server" AlternatingItemStyle-BackColor="Beige"
FooterStyle-BackColor="Silver" FooterStyle-ForeColor="White" ItemStyle-BackColor="White" EditItemStyle-Font-Name="Verdanna"
EditItemStyle-Font-Size="8pt" EditItemStyle-BackColor="yellow" HeaderStyle-BackColor="DarkGreen"
HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="Beige" BorderColor="black" BorderWidth="1"
CellPadding="3" Font-Name="Verdanna" Font-Size="8pt" EnableViewState=False>
<Columns>
<asp:EditCommandColumn HeaderText="Action" EditText="Edit" CancelText="Cancel" ItemStyle-Wrap="False" HeaderStyle-Wrap="False" />
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Name").ToString().Replace("''","'")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn HeaderText="Delete" Text="Delete" CommandName="Delete" />
</Columns>
</asp:datagrid>
 
Where are you filling: Catalog.Tables["Category"].DefaultView;?
You will have to refill your datasource after each postback.
 
I do that in PageLoad event.

private void Page_Load(object sender, System.EventArgs e)
{
//get category data from Session state
Catalog = (DataSet)Session["Catalog"];

if (Catalog==null)
{
//get from database
Catalog = ZoyaModaDP.LoadCatalog();
Session["Catalog"] = Catalog;
}
 
If (catalog==null) is true, then you never repopulate the session variable and therefore Catalog will be empty or NULL.
 
Thats not true, if Catalog==null, I do this:

if (Catalog==null)
{
//get from database
Catalog = ZoyaModaDP.LoadCatalog();
Session["Catalog"] = Catalog;//repopulate session
}

 
Sorry, you are correct, I read over it too quickly. The problem is you are accessing the dataset directly. I'm not sure the scope you put on that variable. You are saving a copy to session. This is what you should be using in the Edit event.

CategoryDataGrid.DataSource = Session["Catalog"];
 
thanks, I will try that. But can you explain why I it shoudl be doen that way instead of the way I did it? The scope of Catalog variable is class-level. Whats wrong with first assigning a dataset from session to class level variable and then doing CategoryDataGrid.DataSource = Catalog.Tables["Category"].DefaultView;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top