473,326 Members | 2,095 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,326 software developers and data experts.

GridView bound DataTable - how to get updates working?

Hello Everyone,

I have a GridView control bound to a plain DataTable object.

AutoGenerateEditButton is set to true, Edit button gets displayed, and
RowEditing event fires as expected.

What do I need to change so that GridView control displays textboxes while
editing and Edit button changes to Cancel button?

What do I need to do to be able to select a row? Setting
AutoGenerateSelectButton to true and defining <SelectedRowStyletemplate
does not seem to be sufficient in this scenario.

Thank you,

Tomasz
Dec 4 '06 #1
4 26673
Hi Tomasz,

Whatever data source object you bind to a control, a data source view
object is created. A data source view object is a class that can perform
SELECT, INSERT, DELETE, and UPDATE operations on a bound object.

If you're binding the GridView to a SqlDataSource or an ObjectDataSource
which incorporates data source view object that fully supports these 4
operations, the GridView will automatically enable editing the data, for
example:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField DataField="ProductID"
HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName"
HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName] FROM
[Alphabetical list of products]"
UpdateCommand="Update [Alphabetical list of products] Set
[ProductName]=@ProductName Where [ProductID]=@ProductID"
>
</asp:SqlDataSource>
However, when you're directly binding to a DataTable which doesn't have a
data source view object, it will be wrapped in a dynamically created data
source view object of type ReadOnlyDataSource--an internal undocumented
class. In such scenario, the automatic editing feature is not possible. You
will have to use template column to setup the editing template and handle
the updating yourself.

Here's a short example on how to do that manually:

<asp:GridView AutoGenerateColumns="false" ID="GridView1"
runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField HeaderText="ID" DataField="ID"
ReadOnly="true" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#
Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}

protected DataTable GetDataSource()
{
const string key = "MyDataSource";
DataTable dt = Session[key] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "first object");
dt.Rows.Add(2, "second object");
Session[key] = dt;
}
return dt;
}
protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
TextBox txtName =
GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
string newname = txtName.Text;

DataTable dt = GetDataSource();
DataRow[] rows = dt.Select("ID = " + id.ToString());
rows[0]["Name"] = newname;
GridView1.EditIndex = -1;
BindGrid();
}
You can find more information here:

#Working with GridView without using Data Source Controls
http://www.dotnetbips.com/articles/d...le.aspx?id=511

Let me know if you need further information. Thanks.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 5 '06 #2
Thank you Walter, I appreciate your helpful response.
Do you also, by a chance, know how to make DetailsView bound to a DataTable
work?

Tomasz
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:F5**************@TK2MSFTNGHUB02.phx.gbl...
Hi Tomasz,

Whatever data source object you bind to a control, a data source view
object is created. A data source view object is a class that can perform
SELECT, INSERT, DELETE, and UPDATE operations on a bound object.

If you're binding the GridView to a SqlDataSource or an ObjectDataSource
which incorporates data source view object that fully supports these 4
operations, the GridView will automatically enable editing the data, for
example:

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField DataField="ProductID"
HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName"
HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName] FROM
[Alphabetical list of products]"
UpdateCommand="Update [Alphabetical list of products] Set
[ProductName]=@ProductName Where [ProductID]=@ProductID"
>
</asp:SqlDataSource>
However, when you're directly binding to a DataTable which doesn't have a
data source view object, it will be wrapped in a dynamically created data
source view object of type ReadOnlyDataSource--an internal undocumented
class. In such scenario, the automatic editing feature is not possible.
You
will have to use template column to setup the editing template and handle
the updating yourself.

Here's a short example on how to do that manually:

<asp:GridView AutoGenerateColumns="false" ID="GridView1"
runat="server" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:BoundField HeaderText="ID" DataField="ID"
ReadOnly="true" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#
Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}

protected DataTable GetDataSource()
{
const string key = "MyDataSource";
DataTable dt = Session[key] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "first object");
dt.Rows.Add(2, "second object");
Session[key] = dt;
}
return dt;
}
protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
TextBox txtName =
GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
string newname = txtName.Text;

DataTable dt = GetDataSource();
DataRow[] rows = dt.Select("ID = " + id.ToString());
rows[0]["Name"] = newname;
GridView1.EditIndex = -1;
BindGrid();
}
You can find more information here:

#Working with GridView without using Data Source Controls
http://www.dotnetbips.com/articles/d...le.aspx?id=511

Let me know if you need further information. Thanks.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Dec 6 '06 #3
Hi Tomasz,

It should be similar to above approach. Based on above example, we can
create a DataView and apply the filter with selected row in GridView to
update the data source that is binding to the DetailsView.

Here's some example code:

<asp:GridView DataKeyNames="ID" AutoGenerateColumns="false" ID="GridView1"
runat="server"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnSelectedIndexChanged="GridView1_SelectedIndexCha nged">
<Columns>
<asp:CommandField ShowEditButton="true" ShowSelectButton="true" />
<asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="true" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#
Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:DetailsView AutoGenerateEditButton="true" AutoGenerateRows="false"
ID="DetailsView1"
runat="server" DataKeyNames="ID"
OnModeChanging="DetailsView1_ModeChanging"
OnItemUpdating="DetailsView1_ItemUpdating">
<Fields>
<asp:BoundField HeaderText="ID" DataField="ID" ReadOnly="true" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#
Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#
Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}

private void BindGrid()
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}

protected DataTable GetDataSource()
{
const string key = "MyDataSource";
DataTable dt = Session[key] as DataTable;
if (dt == null)
{
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "first object");
dt.Rows.Add(2, "second object");
Session[key] = dt;
}
return dt;
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs
e)
{
int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);
TextBox txtName =
GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtName") as TextBox;
string newname = txtName.Text;

FindRowsByID(id)[0]["Name"] = newname;
GridView1.EditIndex = -1;
BindGrid();
}

private DataRow[] FindRowsByID(int id)
{
DataRow[] rows = GetDataSource().Select("ID = " + id.ToString());
return rows;
}

private DataView CreateDataViewByID(int id)
{
DataView dv = new DataView(GetDataSource());
dv.RowFilter = "ID = " + id.ToString();
return dv;
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
BindDetailsView();
}

private void BindDetailsView()
{
int id = (int)GridView1.SelectedValue;
DetailsView1.DataSource = CreateDataViewByID(id);
DetailsView1.DataBind();
}

protected void DetailsView1_ModeChanging(object sender,
DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
BindDetailsView();
}

protected void DetailsView1_ItemUpdating(object sender,
DetailsViewUpdateEventArgs e)
{
int id = (int)DetailsView1.DataKey[0];
TextBox txtName = DetailsView1.FindControl("txtName") as TextBox;
string newname = txtName.Text;
FindRowsByID(id)[0]["Name"] = newname;
BindGrid();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
BindDetailsView();
}

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 6 '06 #4
Hi Tomasz,

This is just a quick note to check the status of this post. What do you
think of above solution on DetailsView? Please feel free to reply here if
there's anything I can help.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 8 '06 #5

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

Similar topics

13
by: Tomasz Jastrzebski | last post by:
Helo All, The problem: GridView control does not render at all (header/footer) when the data source is empty. I have seen a similar question posted already, but I just can not believe there is...
3
by: r.hein | last post by:
I've got a middle tier data object that I'm binding a gridview to, and I'm having some problems with the check box, and having it enabled. Let'd start with the middle tier object (and the...
0
by: rmgalante | last post by:
Hi, I've been experimenting with the ASP.Net GridView and encountered some interesting issues that I thought I would share. I have a page that loads a GridView with a generic collection of...
2
by: eric.dehaan | last post by:
Hello. I have a Gridview on my webpage. To this gridview, I bind a datatable. The results are correctly displayed. But after I edit some values and trigger the event, the values & are empty....
0
by: andrewbb | last post by:
I have an ObjectDataSource with a GridView that displays a DataTable. That works fine, but I'm not sure how to implement the Update. This is a more involved update process than most: 1....
2
by: Tim | last post by:
I'm using a GridView with its DataSource property set to a DataTable. During the RowDataBound event, the first row of the DataTable is reported as a DataControlRowType.Header instead of...
0
by: tina2626 | last post by:
I m using this code in C#.net, for dynamic creation of GridView without using DB. <CODE> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) {...
1
by: Leon Mayne | last post by:
Hello, I have a gridview that's bound to a generic list of business objects and am using the RowCommand event to capture user clicks on actions. This was working fine up until today, but now...
0
by: user1980 | last post by:
Hello Can somebody please help me with the paging in a grid-view. please find my code below SqlDataAdapter sda = new SqlDataAdapter(); DataTable dt = new DataTable(); ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.