Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
5.00/5 (1 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

code

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 + "'";

            //conn.Open();
            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;
            dataAdapter.Fill(table);
            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;
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            try
            {
                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);
            }
            finally
            {
                conn.Close();
            }
	}
Posted
Updated 20-Apr-13 21:21pm
v2

1 solution

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:

XML
<asp:BoundField DataField="Town" HeaderText='Town'
                    SortExpression="Town" />
 
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