谁将数据从sql db显示到具有一个固定列名的datagridview [英] who to display data form sql db to datagridview having one fixed column name

查看:62
本文介绍了谁将数据从sql db显示到具有一个固定列名的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个文本框txtDistrict的表单和一个包含一列clmTown的datagridview



我想显示从db到固定列的数据。我正在使用的代码工作,但为现有的代码创建了额外的列。我怎么去这个



代码



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使用此功能但仍无效。请帮帮我






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();
            }
	}

推荐答案

您应该通过设置< columns>来自定义ASP页面中的GridView。只允许一列(删除其他列),如下例所示:



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" />


这篇关于谁将数据从sql db显示到具有一个固定列名的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆