Click here to Skip to main content
15,891,692 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi..
I have a gridview which column name have custom names and coming from sql command.
I want that add textboxes blow every column and at the end have a insert button when i press insert button then value insert in gridview then new row generated and then enter values and press insert button then values inserted in gridview and so on..

What I have tried:

Here is my aspx.cs code
C#
protected void Page_Load(object sender, EventArgs e)
 {
   load();
  }
  public void load()
  {
           SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
           string query = "SELECT Column_Name FROM tbl_field WHERE Table_Name='std'";
           SqlCommand cmd = new SqlCommand(query, cnn);
           cnn.Open();
           string somevalue = cmd.ExecuteScalar().ToString();
           DataTable dt1 = new DataTable();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           da.Fill(dt1);
           cmd.ExecuteNonQuery();
           int rows = dt1.Rows.Count;
           DataRow dr = null;
           foreach (DataRow row in dt1.Rows)
           {
               DataTable dt2 = new DataTable();
               dt2 = new DataTable();

               for (int i = 0; i < rows; i++)
               {
                   dt2.Columns.Add(dt1.Rows[i][0].ToString(), System.Type.GetType("System.String"));

                   dr = dt2.NewRow();

               }


               Session["MyDataTable"] = dt2;

               DataTable dt3 = (DataTable)Session["MyDataTable"];

               DataRow row1 = dt3.NewRow();

               dt3.Rows.Add(row1);


               Session["MyDataTable"] = dt3;

               GridView1.DataSource = dt3;

               GridView1.DataBind();

               cnn.Close();
           }
       }
   }

and here is my aspx code :
ASP.NET
<body>
    <form id="form1"  runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" >
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:ButtonField CommandName="Cancel" HeaderText="Operation" ShowHeader="True" 
                Text="Insert" />
        </Columns>
    </asp:GridView>
    </div>
    </form>
    
</body>
Posted
Updated 7-Jun-16 23:19pm
Comments
Hameed Khan 29-May-16 4:11am    
But in this article issue is columnName,my column name is dynamic e.g 4,5,6 don't know how much column name becasue column name is change with respect to every table ,we don't know how much user add field in table etc.So,you provide me artciles in which we know how much columns and then we bind data etc.But here columns is dynamic,so how can i resolve this issue
Karthik_Mahalingam 29-May-16 4:14am    
try adding the textbox in the header template.
Hameed Khan 31-May-16 2:52am    
Can You Help me please

1 solution

please see this article .. nicely explained .. to make use of TemplateField..create columns dynamically .. i hope this helps .. let me know if you still need help ..

How to create template columns dynamically in a grid view[^]



Cheers
Praveen
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900