GridView edit when binded to datatable(No DB Source)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: GridView edit when binded to datatable(No DB Source)

  1. #1
    Join Date
    Feb 2008
    Posts
    1

    GridView edit when binded to datatable(No DB Source)

    I have binded my gridview to a datatable. Initally the gridview and datatable will have only one row (Because, min of one row is required to display the gridview). The gridview view is binded to the datatable along with the gridview columns also defined in the pageload.There is an "add" button provided in samepanel as grid, to add new data row into the grid. The new row contents are given through the text boxes in a separate panel beneath the grid. Thus new rows are added into the gridview. Each row has an View image, on click of this image, the row contents should be assigned to the corrosponding text boxes in the panel below. However the problem is, how to capture the row index and the row contents, and populated them in the textboxes.

    Following is the code.

    ------------------ASPX Page Source--------------------
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Notes.aspx.cs" Inherits="Notes" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" >
    
    <head runat="server">
    
    <title>Sample Page</title> 
    </head>
    
    <body>
    
    <form id="form1" runat="server">
    
    <div>
    
    <asp:Panel ID="Panel1" runat="server" BorderColor="Gray" Width="255px" BorderStyle="Solid" BorderWidth="1px">
    
    <asp:Button ID="btnAddNew" runat="server" OnClick="btnAddNew_Click" style="margin-left:180px;"
    
    Text="AddNew" Width="63px" />
    
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" Height="32px" 
    Width="255px" >
    
    <Columns>
    
    <asp:TemplateField>
    
    <ItemTemplate>
    
    <asp:ImageButton id="View" runat="server" OnClick="View_Click" ImageUrl ="~/Images/View.gif" />
    
    </ItemTemplate>
    
    </asp:TemplateField>
    
    </Columns>
    
    </asp:GridView>
    
    </asp:Panel>
    
    </div>
    
    <br />
    
    <br />
    
    <table style="margin-top:0px;left:300px;">
    
    <tr>
    
    <td>
    
    <asp:Panel ID="Panel2" runat="server" BorderColor="Gray" BorderStyle="Solid" height="200px" Width="400px" left="270px" top="0px" BorderWidth="1px" style="left:270px;top:0px">
    
    <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 25px; position: absolute; 
    margin-top:0px" Text="Type" Width="93px"></asp:Label>
    
    <asp:TextBox ID="Message" runat="server" TextMode="MultiLine" style="z-index: 103;left: 25px; position: absolute; 
    margin-top:100px" Width="172px"></asp:TextBox>
    
    <asp:RequiredFieldValidator ID="MessageRF" runat="server" Style=" margin-left:0px;position: absolute;margin-top:100px;" ControlToValidate="Message">*</asp:RequiredFieldValidator> 
    <asp:Label ID="Label2" runat="server" Style="z-index: 101; left: 25px; position: absolute;
    
    margin-top:20px" Text="Message" Width="101px"></asp:Label>
    
    <asp:DropDownList ID="Type" runat="server" Style="z-index: 102; left: 25px; position: absolute;margin-top:20px" Width="151px" >
    
    <asp:ListItem Value="Sales">Sales</asp:ListItem>
    
    <asp:ListItem Value="Billing">Billing</asp:ListItem>
    
    <asp:ListItem Value="Customer">Customer</asp:ListItem>
    
    <asp:ListItem Value="Order">Order</asp:ListItem>
    
    </asp:DropDownList> 
    
    <asp:Button ID="btnAddRow" runat="server" OnClick="btnAddRow_Click" Style="z-index: 105; 
    left: 60px; position: absolute; margin-top:160px" Text="Add" Width="70px" />
    
    <asp:Button ID="btnClose" runat="server" Text="Close" Width="70px" style="z-index: 105; 
    left: 150px; position: absolute; margin-top:160px" OnClick="btnClose_Click"/> 
    
    </asp:Panel>
    
    <br />
    
    </td>
    
    </tr> 
    </table>
    
    </form> 
    </body>
    
    </html>
    -----------------CS Code -----------------------------------
    Code:
     using System;
    
    using System.Data; 
    using System.Configuration;
    
    using System.Collections;
    
    using System.Web;
    
    using System.Web.Security;
    
    using System.Web.UI;
    
    using System.Web.UI.WebControls;
    
    using System.Web.UI.WebControls.WebParts;
    
    using System.Web.UI.HtmlControls;
    
    public partial class Notes : System.Web.UI.Page
    
    {
    
    DataTable table = new DataTable();
    
    DataRow myDataRow;protected void Page_Load(object sender, EventArgs e) 
    {
    
    if (GridView1.Rows.Count == 0) 
    {
    
    DataColumn column = new DataColumn();
    
    column = new DataColumn("Created On");column.DataType = typeof(string); 
    table.Columns.Add(column); 
    
    column = new DataColumn("Type");column.DataType = typeof(string); 
    table.Columns.Add(column); 
    
    column = new DataColumn("Message");column.DataType = typeof(string); 
    table.Columns.Add(column);
    
    ViewState["tableSave"] = table;
    
    Panel2.Visible = false; 
    }
    
    if (IsPostBack != true) 
    { //since a min of one row is required, hence adding this.
    
    for (int i = 0; i < 1; i++) 
    {
    
    myDataRow = table.NewRow();
    
    table.Rows.Add(myDataRow);
    
    }
    
    GridView1.DataSource = table;
    
    GridView1.DataBind();
    
    }
    
     
    
    }
    
    protected void btnAddNew_Click(object sender, EventArgs e) 
    {
    
    Panel2.Visible = true; 
    }
    
    protected void btnAddRow_Click(object sender, EventArgs e) 
    {
    
    table = (DataTable)ViewState["tableSave"]; 
    DataRow row = table.NewRow();
    
    row["Created On"] = DateTime.Now; 
    row["Type"] = Type.Text;
    
    row["Message"] = Message.Text; 
    table.Rows.Add(row);
    
    GridView1.DataSource = table;
    
    GridView1.DataBind();
    
    Type.Text = ""; 
    Message.Text = "";
    
    Panel2.Visible = false; 
     
    
    }
    
    protected void View_Click(object sender, EventArgs e) 
    {
    
    Panel2.Visible = true;
    
    //Not sure how can i capture the individual row content and populate in their corrosponding textboxes.  
    
    }
    
    protected void btnClose_Click(object sender, EventArgs e) 
    {
    
    Panel2.Visible = false; 
    }
    
    }
    --------------------------Appreciate any help on this

  2. #2
    Join Date
    Feb 2008
    Posts
    1
    Okay you first problem is that you're using that funny language with square bracket :-)

    No seriously, I used a comand field using the same image so it looks identical.
    and used SelectedIndexChanged event of the grid and used the same code you started with.

    Panel2.Visible = true;
    GridViewRow rw = GridView1.SelectedRow;
    Messge.text = rw.Cells[4].Text;

    That's it

    Page

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Notes.aspx.cs" Inherits="Notes" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Sample Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="Panel1" runat="server" BorderColor="Gray" Width="255px" BorderStyle="Solid" BorderWidth="1px">
    <asp:GridView ID="GridView1" runat="server" Height="32px"
    onselectedindexchanged="GridView1_SelectedIndexChanged" Width="255px">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:ImageButton ID="View" runat="server" ImageUrl="~/Images/View.gif"
    OnClick="View_Click" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField ButtonType="Image" SelectImageUrl="~/Images/view.gif"
    ShowSelectButton="True" />
    </Columns>
    </asp:GridView>
    <asp:Button ID="btnAddNew" runat="server" OnClick="btnAddNew_Click" style="margin-left:180px;"
    Text="AddNew" Width="63px" />
    </asp:Panel>
    </div>
    <br />
    <br />
    <table style="margin-top:0px;left:300px;">
    <tr>
    <td>
    <asp:Panel ID="Panel2" runat="server" BorderColor="Gray" BorderStyle="Solid" height="200px" Width="400px" left="270px" top="0px" BorderWidth="1px" style="left:270px;top:0px">
    <asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 25px; position: absolute;
    margin-top:0px" Text="Type" Width="93px"></asp:Label>
    <asp:TextBox ID="Message" runat="server" TextMode="MultiLine" style="z-index: 103;left: 25px; position: absolute;
    margin-top:100px" Width="172px"></asp:TextBox>
    <asp:RequiredFieldValidator ID="MessageRF" runat="server" Style=" margin-left:0px;position: absolute;margin-top:100px;" ControlToValidate="Message">*</asp:RequiredFieldValidator>
    <asp:Label ID="Label2" runat="server" Style="z-index: 101; left: 25px; position: absolute;
    margin-top:20px" Text="Message" Width="101px"></asp:Label>
    <aspropDownList ID="Type" runat="server" Style="z-index: 102; left: 25px; position: absolute;margin-top:20px" Width="151px" >
    <asp:ListItem Value="Sales">Sales</asp:ListItem>
    <asp:ListItem Value="Billing">Billing</asp:ListItem>
    <asp:ListItem Value="Customer">Customer</asp:ListItem>
    <asp:ListItem Value="Order">Order</asp:ListItem>
    </aspropDownList>
    <asp:Button ID="btnAddRow" runat="server" OnClick="btnAddRow_Click" Style="z-index: 105;
    left: 60px; position: absolute; margin-top:160px" Text="Add" Width="70px" />
    <asp:Button ID="btnClose" runat="server" Text="Close" Width="70px" style="z-index: 105;
    left: 150px; position: absolute; margin-top:160px" OnClick="btnClose_Click"/>
    </asp:Panel>
    <br />
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

    Code behind

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    public partial class Notes : System.Web.UI.Page
    {
    DataTable table = new DataTable();
    DataRow myDataRow;

    protected void Page_Load(object sender, EventArgs e)
    {
    if (GridView1.Rows.Count == 0)
    {
    DataColumn column = new DataColumn();
    column = new DataColumn("Created On");column.DataType = typeof(string);
    table.Columns.Add(column);
    column = new DataColumn("Type");column.DataType = typeof(string);
    table.Columns.Add(column);
    column = new DataColumn("Message");column.DataType = typeof(string);
    table.Columns.Add(column);
    ViewState["tableSave"] = table;
    Panel2.Visible = false;
    }
    if (IsPostBack != true)
    { //since a min of one row is required, hence adding this.
    for (int i = 0; i < 1; i++)
    {
    myDataRow = table.NewRow();
    table.Rows.Add(myDataRow);
    }
    GridView1.DataSource = table;
    GridView1.DataBind();
    }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
    Panel2.Visible = true;
    }
    protected void btnAddRow_Click(object sender, EventArgs e)
    {
    table = (DataTable)ViewState["tableSave"];
    DataRow row = table.NewRow();
    row["Created On"] = DateTime.Now;
    row["Type"] = Type.Text;
    row["Message"] = Message.Text;
    table.Rows.Add(row);
    GridView1.DataSource = table;
    GridView1.DataBind();
    Type.Text = "";
    Message.Text = "";
    Panel2.Visible = false;
    }
    protected void View_Click(object sender, EventArgs e)
    {
    Panel2.Visible = true;
    //Not sure how can i capture the individual row content and populate in their corrosponding textboxes.

    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
    Panel2.Visible = false;
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    Panel2.Visible = true;
    GridViewRow rw = GridView1.SelectedRow;
    Message.Text = rw.Cells[4].Text;
    }
    }

    This is much easier in VB :-) (not really)

    Regards,
    Angel

Similar Threads

  1. Replies: 0
    Last Post: 04-02-2007, 10:39 AM
  2. Replies: 1
    Last Post: 10-21-2005, 09:49 AM
  3. runtime.exec()
    By Seb in forum Java
    Replies: 7
    Last Post: 10-18-2005, 05:14 PM
  4. Why is this in the console?
    By Dark Rain in forum Java
    Replies: 8
    Last Post: 09-30-2005, 06:42 PM
  5. Replies: 5
    Last Post: 07-19-2001, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center