Newbie : Updating problem with Datagrid

  • Thread starter Thread starter ycquak
  • Start date Start date
Y

ycquak

Hi,

I have a page with a datagrid control. What I would like to do is when
I click on the update statement, I will update to the datagrid. However
the problem I am facing currently is when I click on update, the value
which I got from the controls generated by the datagrid edit template
is empty. Is there something I am missing out on? I have attached the
source codes below, please advice. Thanks!

Web User Control :

<asp:DataGrid id="participateGrid" runat="server"
AutoGenerateColumns="False" CellPadding="1" CellSpacing="1"
Width="100%" BorderColor="White" BorderStyle="None"
OnEditCommand="Edit" OnDeleteCommand="Delete" OnUpdateCommand="Update"
ShowHeader="True">
<HeaderStyle BackColor="#dedfde" CssClass="tableLabel"></HeaderStyle>
<ItemStyle CssClass="tableItem" BackColor="#f7e7e7"></ItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="Outlet">
<EditItemTemplate>
<asp:DropDownList ID="ddlOutlet" Runat="server">
<asp:ListItem>...</asp:ListItem>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Remarks">
<EditItemTemplate>
<asp:TextBox id="txtRemarks" TextMode="MultiLine" Rows="2"
Columns="15" Runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" EditText="<img
src='editimage.jpg'>" UpdateText="<img src='updateimage.gif'>">

</asp:EditCommandColumn>
<asp:ButtonColumn CommandName="Delete" ButtonType="LinkButton"
Text="<img src='delete.gif'"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<asp:ImageButton ImageUrl="image.gif" id="btn_new"
runat="server"></asp:ImageButton>


This is the codebehind file update method :

protected void Update (object sender, DataGridCommandEventArgs e)
{
DropDownList outlet = (DropDownList)e.Item.Cells[0].FindControl
("ddlOutlet");
TextBox remarks = ((TextBox)e.Item.Cells[1].FindControl
("txtRemarks"));

DataRow dr = dt.Rows[e.Item.DataSetIndex];

dr["Outlet"] = outlet.SelectedItem.Text;
dr["Remarks"] = remarks.Text;

saveData();

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

private void saveData ()
{
//stores the information in a viewstate
ViewState ["Participate"] = dt;
}

//bind the data
private void BindGrid()
{
participateGrid.DataSource = dv;
participateGrid.DataBind();
}
 
Are u binding your datagrid on page load? Page load event occurs before
datagrid update event.
If your answer is yes, control postback on your page load something like
this:

if (!IsPostBack)
{
BindGrid();
}
 
Below is my page load event. I suspect something is wrong with the
loadData method, is it?

private void Page_Load(object sender, System.EventArgs e)
{

if (ViewState ["Participate"] == null)
{
loadData();
ViewState["Participate"] = dt;
}

else
{
dt = (DataTable)ViewState["Participate"];
dv = new DataView (dt);
}

if (!IsPostBack)
{
Session ["ParticipateCounter"] = gridCounter;
}
else
{
gridCounter = (int)Session ["ParticipateCounter"];
}

BindGrid();
}

//page load event

//initialise the local objects
private void loadData ()
{
dt = new DataTable();
dt.Columns.Add (new DataColumn ("Outlet", typeof(string)));
dt.Columns.Add (new DataColumn ("Remarks", typeof(string)));

dv = new DataView(dt);

}
 
yep, got it! Thanks! Managed to get the right values.
Hmm, maybe would just like to ask another question. Why is it that when
I click on update, after posting back, I still get the edit box?

I have set my EditItemIndex to -1 already. Why is it not being set to
-1? Thanks!

Regards
Yew Chong
 
Surely it caused by event order. Everytime event order is as follows.
1. Page_Load
2. Click events
3. Changed events
Every time the page is posted client. Firstly your datagrid is binded then
events like datagrid events are handled. So edit item index is set after the
datagrid is binded.
 
Thanks a lot! I got this working as well.
Really thanks for the time taken to answer my questions.
 
Back
Top