Click here to Skip to main content
13,148,427 members (62,383 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have a form which contains one textbox txtDistrict and a datagridview with one column clmTown

i want to display data from the db to the fixed column. the code i am using works but creates additional column to the exsisting one. how do i go about this


public void studentmoney()
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            string strConn = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
            string strSQL = "select Town from districtnt where District = '" + txtDistrict.Text + "'";
            SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConn);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
            // Populate a new data table and bind it to the BindingSource.
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            bindingSource1.DataSource = table;
            // you can make it grid readonly.
            dgvDistrict.ReadOnly = true;
            // finally bind the data to the grid
            dgvDistrict.DataSource = bindingSource1;

itry using this but still does not works. please help me out

private void txtDistrict_SelectedIndexChanged(object sender, EventArgs e)
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            conn.ConnectionString = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
            string sqlQuery = null;
            sqlQuery = "select * from DistrictnT where District = '" + txtDistrict.Text + "'";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
            cmd.CommandType = System.Data.CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                    dgvDistrict.Rows[dgvDistrict.Rows.Count - 1].Cells["clmTowns"].Value = dr[2].ToString();
            catch (Exception ex)
                MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Posted 20-Apr-13 20:39pm
Updated 20-Apr-13 21:21pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should customize your GridView in the ASP page by settings <columns> by letting there only one column (remove the others) like in the next example:

<asp:BoundField DataField="Town" HeaderText='Town'
                    SortExpression="Town" />

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100