使用另一个选择查询刷新DataGridView

时间:2017-10-08 15:23:05

标签: c# datagridview

我希望能够在我的应用程序中选择一个视图,其中一个选择应该获得Imported = 1的所有行,而另一个选择应该只获得Imported = 0.我刚开始学习这个,所以我去了来自Microsoft https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-bind-data-to-the-windows-forms-datagridview-control的例子但我不想要数据库中的所有列,我也希望在某些列上有一定的宽度,所以我添加了列代码,它一直很好用。但是当我尝试做一个新的GetData它不刷新时,它会添加记录。 GetData作为一个刷新在Microsofts示例中运行良好,所以我猜测这与我在datagridview中创建列的方式有关。我的代码出了什么问题? btnZendeskImport_Click是这种情况下的刷新。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private BindingSource bindingSource1 = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        DataTable table = new DataTable();

        public Form1()
        {
            InitializeComponent();
            LoadDGW();
            dataGridView1.DataSource = bindingSource1;
            GetData("select TicketID,Customer,Subject,ticketTime,ticketDate,Assignee,ProjectType,SupportAgreement,Imported from Ticketslabb WHERE Imported = 1");
            testLabel.Text = dataGridView1.RowCount.ToString();
        }
        private void GetData(string selectCommand)
        {
            try
            {
                // Specify a connection string. Replace the given value with a 
                // valid connection string for a Northwind SQL Server sample
                // database accessible to your system.
                String connectionString =
                    "Data Source=LOCALHOST\\SQLEXPRESS;" +
                              "Initial Catalog=ticketDB;" +
                              "Integrated Security=SSPI;";

                // Create a new data adapter based on the specified query.
                dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

                // Create a command builder to generate SQL update, insert, and
                // delete commands based on selectCommand. These are used to
                // update the database.
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

                // Populate a new data table and bind it to the BindingSource.

                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(table);
                bindingSource1.DataSource = table;

                // Resize the DataGridView columns to fit the newly loaded content.
                //dataGridView1.AutoResizeColumns(
                //    DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " +
                    "connectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }
        private void reloadButton_Click(object sender, System.EventArgs e)
        {
            //Reload the data from the database.
            GetData(dataAdapter.SelectCommand.CommandText);
            MessageBox.Show(dataGridView1.SelectedRows.Count.ToString());
        }
        private void searchTextBox_KeyPress(object sender, EventArgs e)
        {
            table.DefaultView.RowFilter = string.Format("CONVERT({0}, System.String) like '%{1}%' OR Subject like '%{1}%' OR Customer like '%{1}%'", "TicketID", searchTxtBox.Text);
        }
        private void LoadDGW()
        {
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.Columns.AddRange(

                new cbComponent.GridViewCheckBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewTextBoxColumn(),
                new DataGridViewCheckBoxColumn());

            DataGridViewColumn dgwTicketCheckbox = dataGridView1.Columns[0];
            DataGridViewColumn dgwTicketID = dataGridView1.Columns[1];
            DataGridViewColumn dgwTicketCustomer = dataGridView1.Columns[2];
            DataGridViewColumn dgwTicketSubject = dataGridView1.Columns[3];
            DataGridViewColumn dgwTicketTime = dataGridView1.Columns[4];
            DataGridViewColumn dgwTicketDate = dataGridView1.Columns[5];
            DataGridViewColumn dgwTicketAgent = dataGridView1.Columns[6];
            DataGridViewColumn dgwTicketProjectType = dataGridView1.Columns[7];
            DataGridViewColumn dgwTicketSupportAgreement = dataGridView1.Columns[8];
            DataGridViewColumn dgwTicketImported = dataGridView1.Columns[9];


            dgwTicketCheckbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dgwTicketCheckbox.Width = 30;

            dgwTicketID.Name = "dgwTicketID";
            dgwTicketID.HeaderText = "ID";
            dgwTicketID.DataPropertyName = "TicketID";
            dgwTicketID.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dgwTicketID.Width = 50;

            dgwTicketCustomer.Name = "dgwTicketCustomer";
            dgwTicketCustomer.HeaderText = "Kund";
            dgwTicketCustomer.DataPropertyName = "Customer";
            dgwTicketCustomer.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dgwTicketCustomer.Width = 210;

            dgwTicketSubject.Name = "dgwTicketSubject";
            dgwTicketSubject.HeaderText = "Ämne";
            dgwTicketSubject.DataPropertyName = "Subject";
            dgwTicketSubject.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dgwTicketSubject.Width = 420;

            dgwTicketTime.Name = "dgwTicketTime";
            dgwTicketTime.HeaderText = "Tid";
            dgwTicketTime.DataPropertyName = "ticketTime";
            dgwTicketTime.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dgwTicketTime.Width = 40;

            dgwTicketDate.Name = "dgwTicketDate";
            dgwTicketDate.HeaderText = "Datum";
            dgwTicketDate.DataPropertyName = "ticketDate";

            dgwTicketAgent.Name = "dgwTicketAgent";
            dgwTicketAgent.HeaderText = "Agent";
            dgwTicketAgent.DataPropertyName = "Assignee";

            dgwTicketProjectType.Name = "dgwProjectType";
            dgwTicketProjectType.DataPropertyName = "ProjectType";
            dgwTicketProjectType.Visible = false;


            dgwTicketSupportAgreement.Name = "dgwTicketSupportAgreement";
            dgwTicketSupportAgreement.DataPropertyName = "SupportAgreement";
            dgwTicketSupportAgreement.Visible = false;

            dgwTicketImported.Name = "dgwTicketImported";
            dgwTicketImported.DataPropertyName = "Imported";
            dgwTicketImported.Visible = false;

            dataGridView1.RowHeadersVisible = false;
            dataGridView1.MultiSelect = true;
            this.Controls.Add(dataGridView1);
        }

        private void btnVismaChecked_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value) == true)
                {
                    var builder = new StringBuilder();
                    row.Cells.Cast<DataGridViewCell>()
                                   .ToList().ForEach(cell =>
                                   {
                                       if (cell.ColumnIndex != 0)
                                       {
                                           builder.Append(string.Format("{0} ", cell.Value));
                                       }
                                   });
                    rows_with_checked_column.Add(row);
                    MessageBox.Show(builder.ToString());
                }
            }
        }

        private void cbView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnZendeskImport_Click(object sender, EventArgs e)
        {

            GetData("select TicketID,Customer,Subject,ticketTime,ticketDate,Assignee,ProjectType,SupportAgreement,Imported from Ticketslabb WHERE Imported = 0");
            testLabel.Text = dataGridView1.RowCount.ToString();
        }
    }
}

0 个答案:

没有答案