.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

อยากถามเรื่อง GridView คับ Options · View
herobig
Posted: Tuesday, September 16, 2008 12:50:43 AM
Rank: มือสมัครเล่น
Groups: Member

Joined: 9/11/2008
Posts: 12

อยากให้ใน Gridview มีคอลัมที่ให้กรอกค่า textbox คับ เช่น ผม select DataBase แสดงดังนี้

+----------------+
| name | age  +
+----------------+
|  A   | textBox |
|  B   | textBox |
+----------------+
แล้วพอกดปุ่ม submit แล้วให้ เก็บค่า age ไว้ใน dataBase คับ

ขอบคุณคับ

ZeedZard
Posted: Wednesday, September 17, 2008 1:10:13 PM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/12/2008
Posts: 50
Location: King Mongkut's Institute of Technology Ladkrabang

ประยุกต์เอานะครับ

 

 

DataTable Table = new DataTable();
    DataRow Row;
    DataColumn Column;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            Column = new DataColumn();
            Column.ColumnName = "Name";
            Table.Columns.Add(Column);
            Column = new DataColumn();
            Column.ColumnName = "Age";
            Table.Columns.Add(Column);
            Row = Table.NewRow();
            Row["Name"] = "A";
            Table.Rows.Add(Row);
            Row = Table.NewRow();
            Row["Name"] = "B";
            Table.Rows.Add(Row);
            GridView1.DataSource = Table;
            GridView1.DataBind();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Table = new DataTable();
        Table = getData();
        // จากนั้นเอา DataTable ที่ได้ไปทำการเซฟ
        foreach (DataRow dr in Table.Rows)
        {
            Response.Write(dr["Name"]+"<br/>");
            Response.Write(dr["Age"]+"<br/>");
        }
        ///
    }
    private DataTable getData()
    {
        DataTable DT = new DataTable(); 
        DataRow DR;
        string[] strCol = { "Name", "Age" };
        foreach (string str in strCol)
        {
            if (!DT.Columns.Contains(str))
            {
                DT.Columns.Add(str);
            }
        }
        foreach (TableRow tRow in GridView1.Rows)
        {           
            DR = DT.NewRow();
           
            foreach (string str in strCol) 
            {                 
                 switch (str) 
                 { 
                  case "Name":
                      DR[str] = (string)((Label)tRow.FindControl(str)).Text; 
                      break; 
                  case "Age":
                      string a = (string)((TextBox)tRow.FindControl(str)).Text;
                      DR[str] = (string)((TextBox)tRow.FindControl(str)).Text; 
                      break;                 
                 } 
            }
            DT.Rows.Add(DR);
        } 
        return DT;
     }
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by