小编Af'*_*faq的帖子

无法使用c#在Winform中使用空ValueMember在ListControl中设置SelectedValue

我有一个组合框,可从数据库加载数据,但出现一个错误,尽管我已将selectValue设置为表的主键,但无法在ListControl中设置selectedValue。但是仍然在运行时出现错误。这是代码。

    private void FormAddStudent_Load(object sender, EventArgs e)
    {
        //For combobox Campuse
        cBoxCampus.DataSource = GetAllCampuses();

        cBoxCampus.DisplayMember = "campus_name";
        cBoxCampus.SelectedValue = "campus_id";
        //Foe ComboBox Department
        cBoxDepartment.DataSource = GetAllDepartment();

        cBoxDepartment.DisplayMember = "depname"; 
        cBoxDepartment.SelectedValue = "depid";
    }
Run Code Online (Sandbox Code Playgroud)

这是插入按钮背后的代码

        private void btnInsert_Click(object sender, EventArgs e)
    {
        string CS = ConfigurationManager.ConnectionStrings["UMSdbConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT ISNULL(MAX(std_id),0)+1 FROM Student", con);
            cmd.CommandType = CommandType.Text;
            tbID.Text = cmd.ExecuteScalar().ToString();

            {

                using (SqlCommand cmd1 = new SqlCommand("INSERT INTO Student (std_id,std_name,std_f_name,std_mob,std_gender,std_cnic,std_campus,std_dep,std_address,std_batch,std_batch_year)VALUES(@std_id,@std_name,@std_f_name,@std_mob,@std_gender,@std_cnic,@std_campus,@std_dep,@std_address,@std_batch,@std_batch_year)VALUES(@campus_id,@campus_name)", con))
                { …
Run Code Online (Sandbox Code Playgroud)

c# winforms

4
推荐指数
1
解决办法
2366
查看次数

如何禁用双击DataGridView的标题

我想做一些事情,同时双击Gridview(行)而不是单个单元格。表示双击事件处理程序,而不表示MouseDoubleClick事件。但是我无法禁用标题列和行双击事件..并且当我双击GridView行时也想将数据加载到combobox(ComboBox处于相同形式)。请帮帮我..!!!

private void gvLoadAllData_DoubleClick(object sender, EventArgs e)
{
    if()
    {
        //Do Something
    }
}
Run Code Online (Sandbox Code Playgroud)

c# datagridview winforms

4
推荐指数
2
解决办法
4898
查看次数

字符串未被识别为C#RadioButton中的有效布尔值

我想将值从一个表单发送到另一个表单,这意味着当我点击一个按钮然后我想通过id列将学生值发送到另一个表单.但是,当我想发送RadioButton值时,我收到有关字符串的错误.

请注意,我不将RadioButton值保存为字符串,即男性或女性进入数据库.

这是代码本身.

if (this.isupdate)
{
    DataTable dtStudentInfo =  GetAllStudentInfoById(this.StudentId);
    DataRow row = dtStudentInfo.Rows[0];
    tbID.Text = row["std_id"].ToString();
    tbName.Text = row["std_name"].ToString();
    tbFatherName.Text = row["std_f_name"].ToString();
    tbAddress.Text = row["std_address"].ToString();
    tbBatchNo.Text = row["std_batch"].ToString();
    tbBatchYear.Text = row["std_batch_year"].ToString();
    tbCNIC.Text = row["std_cnic"].ToString();
    tbMobNumber.Text = row["std_mob"].ToString();
    rbMale.Checked = (row["std_gender"] is DBNull) ? false : Convert.ToBoolean(row["std_gender"] = 1) ? true : false);

}
Run Code Online (Sandbox Code Playgroud)

c# winforms

2
推荐指数
1
解决办法
596
查看次数

如何基于多个选项过滤DataGridView?

我有一个名为dgvAllBikes的gridView控件.在表单加载时,我加载dgvAllBikes.现在我想基于三个选项和一个搜索按钮过滤这个基于dgvAllBikes的gridView.

1)CC

2)模型

3)颜色

当我点击搜索按钮时,我想根据这三个选项过滤dgvAllBikes.

这是我的LoadGrid代码

 private DataTable LoadGrid()
    {

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("spGetAllBikes", con);
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader r = cmd.ExecuteReader();
            dtAllBike = new DataTable();

            dtAllBike.Load(r);

        }
        return dtAllBike;
    }
Run Code Online (Sandbox Code Playgroud)

我在类级别声明了DataTable

string CS;
    protected DataTable dtAllBike;
    public SaleBike()
    {
        InitializeComponent();
        CS = ConfigurationManager.ConnectionStrings["BikeDB"].ConnectionString;
    }
Run Code Online (Sandbox Code Playgroud)

这是图片 在此输入图像描述

这是btnSearch的代码.

private void btnSearch_Click(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = dgvAllBikeDetails.DataSource;
        bs.Filter = dgvAllBikeDetails.Columns["CC"].HeaderText.ToString() + " LIKE '%" + tbCCSearch.Text …
Run Code Online (Sandbox Code Playgroud)

c# gridview datagridview winforms

1
推荐指数
1
解决办法
1647
查看次数

使用SQLCommand在C#中进行SQL多行查询

我想使用C#从SQL Server中的不同表加载特定数据但是我得到了一个错误Incorrect syntax near the keyword 'as'. Incorrect syntax near 'EP'. Incorrect syntax near the keyword 'AND'.代码运行良好,因为我已经放置了一个messageBox来显示查询,一个messageBox popsup并显示完整查询但是之后我收到了一个错误我已经在上面提到过这是代码

 private void FillGridView()
    {
        CS = ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        { SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                SqlDataAdapter ad = new SqlDataAdapter(cmd);
                ad.Fill(dt);
                gvShowAllData.DataSource = dt;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是查询

    string query = @"Select 'ACTIVE POSTING' as POSTING,e.emp_id,e.emp_name,e.emp_fathername,e.emp_nic,e.emp_contact" +
",D.desig_id,D.desig_name" +
"from EMP_Master as e,EMP_Posting_Log as p,EMP_Designation AS D" +
"where e.emp_id=p.emp_id" +
"AND P.desg_id=D.desig_id" +
"and …
Run Code Online (Sandbox Code Playgroud)

c# sql-server

0
推荐指数
2
解决办法
1277
查看次数

标签 统计

c# ×5

winforms ×4

datagridview ×2

gridview ×1

sql-server ×1