Update a DataRow by row index in a DataTable
UpdateDataRowByRowIndex.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html>
<script runat="server">
void Button1_Click(object sender, System.EventArgs e)
{
DataTable dt = new DataTable();
dt.TableName = "Books";
DataColumn dc1 = new DataColumn();
dc1.ColumnName = "BookID";
dc1.DataType = typeof(int);
dc1.AllowDBNull = false;
dc1.Unique = true;
DataColumn dc2 = new DataColumn();
dc2.ColumnName = "BookName";
dc2.DataType = typeof(string);
DataColumn dc3 = new DataColumn();
dc3.ColumnName = "Author";
dc3.DataType = typeof(string);
dt.Columns.AddRange(new DataColumn[] { dc1,dc2,dc3 });
dt.Rows.Add(new object[] { 1, "Windows 7: The Definitive Guide", "William Stanek" });
dt.Rows.Add(new object[] { 2, "Windows 7: Up and Running", "Wei-Meng Lee" });
dt.Rows.Add(new object[] { 3, "Windows® 7 Administrator's Pocket Consultant", "William Stanek" });
GridView1.DataSource = dt;
GridView1.DataBind();
object[] rowArray = new object[3];
rowArray[0] = 10;
rowArray[1] = "Microsoft® SQL Server® 2008 Step by Step";
rowArray[2] = "Mike Hotek";
DataRow tempRow = dt.Rows[1];
tempRow.ItemArray = rowArray;
Label1.Text = "After updating the DataRow in DataTable by row index 1";
GridView2.DataSource = dt;
GridView2.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to update DataRow by row index in a DataTable in ado.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:DarkBlue; font-style:italic;">
How to update DataRow by row
<br /> index in a DataTable in ado.net
</h2>
<hr width="350" align="left" color="CornFlowerBlue" />
<asp:GridView
ID="GridView1"
runat="server"
BorderColor="Snow"
ForeColor="Snow"
Width="550"
>
<HeaderStyle BackColor="Purple" Height="35" />
<RowStyle BackColor="Magenta" />
<AlternatingRowStyle BackColor="DeepPink" />
</asp:GridView>
<br />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Medium"
ForeColor="SeaGreen"
>
</asp:Label>
<br /><br />
<asp:GridView
ID="GridView2"
runat="server"
BorderColor="Snow"
ForeColor="Snow"
Width="550"
ShowHeaderWhenEmpty="true"
>
<HeaderStyle BackColor="Purple" Height="35" />
<RowStyle BackColor="Magenta" />
<AlternatingRowStyle BackColor="DeepPink" />
</asp:GridView>
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="Populate GridView"
Height="45"
Font-Bold="true"
ForeColor="DarkBlue"
/>
</div>
</form>
</body>
</html>
- How to count columns in a DataTable
- How to get column names from a DataTable
- How to get index of a column by name in a DataTable
- How to get maximum size of a column in a DataTable
- How to add a new row into DataTable
- How to delete a row from a DataTable
- How to count rows in a DataTable
- How to update all rows in a DataTable
- How to select data from a DataTable
- How to set all the values for a DataRow through an array