I want to retrieve a data from database, after modifying the record then I want to show it in Gridview.
I already created column names through Edit columns
those names are Date,Location,Job Title,Details
.
This is my asp.net code
<asp:GridView ID="GridViewRecord" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField HeaderText="Date" />
<asp:BoundField HeaderText="Location" />
<asp:BoundField HeaderText="Job Title" />
<asp:BoundField HeaderText="Experience" />
<asp:HyperLinkField HeaderText="Details" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
Then I try to add one sample record directly. But I am getting an error. This is my c# code on page load
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dr[0] = "12-12-12"; //Error message occured here
dr[1] = "Jeddah";
dr[2] = "Java";
dr[3] = "2";
dr[4] = "View Details";
dt.Rows.Add(dr);
GridViewRecord.DataSource = dt;
GridViewRecord.DataBind();
}
}
Error message:
An exception of type 'System.IndexOutOfRangeException' occurred in` System.Data.dll but was not handled in user code
Additional information: Cannot find column 0.
I am new to c#, thanks