none
How to insert textbox value to gridview ? RRS feed

  • Question

  • I have the below code and i don`t know how to insert the textbox value to gridview .On click i want to ad the value from text box to the griview values that i get from the database.

     private DataTable PivotTable(DataTable origTable)
        {
            DataTable newTable = new DataTable();
            DataRow dr = null;
            //Add Columns to new Table
            for (int i = 0; i <= origTable.Rows.Count; i++)
            {
                newTable.Columns.Add(new DataColumn(origTable.Columns[i].ColumnName, typeof(String)));
            }
            //Execute the Pivot Method
            for (int cols = 0; cols < origTable.Columns.Count; cols++)
            {
                dr = newTable.NewRow();
                for (int rows = 0; rows < origTable.Rows.Count; rows++)
                {
                    if (rows < origTable.Columns.Count)
                    {
                        dr[0] = origTable.Columns[cols].ColumnName; // Add the Column Name in the first Column
                        dr[rows + 1] = origTable.Rows[rows][cols];
                    }
                }
                
                newTable.Rows.Add(dr); //add the DataRow to the new Table rows collection
            }
            return newTable;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string str = @"xxxxxxx";
    
                SqlCommand xp = new SqlCommand(str, con);
                xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
                con.Open();
                xp.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = xp;
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    DataTable pivotedTable = PivotTable(dt);
                    BindGrid(pivotedTable, true);
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                con.Close();
            }
        }
        private void BindGrid(DataTable dt, bool rotate)
        {
            GridView2.ShowHeader = !rotate;
            GridView2.DataSource = dt;
            GridView2.DataBind();
            if (rotate)
            {
                foreach (GridViewRow row in GridView1.Rows)
                {
                    row.Cells[0].CssClass = "header";
                }
            }
        }
    
        private string GridViewToHtml(GridView grid)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            grid.RenderControl(hw);
            return sb.ToString();
    
        }
    
        protected void Button2_Click(object sender, EventArgs e)
        {
    
            string Mail_From = "xxxxx";
            string password = "xxxxx";
            string Mail_Subject = TextBox4.Text;
            string Mail_TO = "xxxxxx";
            string Mail_Body = GridViewToHtml(GridView2);//+ " " + "Telefon BT : 0749.210.730 Daniel Tou" + "  " + " ; " + "Telefon in locatie :" + "   " + TextBox3.Text + "   " + " ; " + " Motiv Incident: " + " " + TextBox2.Text;
    
    
            MailMessage MM = new MailMessage(Mail_From, Mail_TO, Mail_Subject, Mail_Body);
            MM.IsBodyHtml = true;
            SmtpClient sc = new SmtpClient();
            sc.Host = "xxxx";
            sc.Port = xxxx;
            sc.Credentials = new System.Net.NetworkCredential(Mail_From, password);
            sc.EnableSsl = true;
            sc.Send(MM);
    
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert ('E-mail trimis !!!');", true);
        }
    
    
     

    Tuesday, March 21, 2017 9:51 PM

All replies

  • Hi Danie Tou,

    Thank you for posting here.

    For your question, you could try the following code top set the value from textBox to the location you want.

      private void button2_Click(object sender, EventArgs e)
            {
                dataGridView1.Rows[0].Cells[1].Value = textBox1.Text;
            }

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 22, 2017 6:36 AM
  • Hi Danie Tou,

    Adding to Wendy's answer, I would like to point out where you might be going wrong. iI your button click the functionality you written is for INSERT operation to database. So your code is taking value from textbox and preparing it for INSERT query.

    protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string str = @"xxxxxxx";
    
                SqlCommand xp = new SqlCommand(str, con);
                xp.Parameters.Add("@search", SqlDbType.NVarChar).Value = TextBox1.Text;
                con.Open();
                xp.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = xp;
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    DataTable pivotedTable = PivotTable(dt);
                    BindGrid(pivotedTable, true);
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                con.Close();
            }
        }


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Wednesday, March 22, 2017 12:10 PM
  • GridView is an ASP.NET control, not a Winforms one. DataBind is only defined on the ASP.NET control.

    Please post ASP.NET questions in the ASP.NET forums.

    Wednesday, March 22, 2017 3:17 PM