Click here to Skip to main content
15,889,116 members
Home / Discussions / C#
   

C#

 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
jschell27-Apr-14 9:36
jschell27-Apr-14 9:36 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
Ravi Bhavnani28-Apr-14 4:44
professionalRavi Bhavnani28-Apr-14 4:44 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
thatraja27-Apr-14 20:59
professionalthatraja27-Apr-14 20:59 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
Dnyaneshwar@Pune28-Apr-14 0:11
Dnyaneshwar@Pune28-Apr-14 0:11 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
rutja_deore28-Apr-14 0:12
rutja_deore28-Apr-14 0:12 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
thatraja28-Apr-14 4:29
professionalthatraja28-Apr-14 4:29 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
Ravi Bhavnani28-Apr-14 4:43
professionalRavi Bhavnani28-Apr-14 4:43 
QuestionRunning thread to refresh image disable scrollbars in DataGridView Pin
neualex26-Apr-14 5:44
neualex26-Apr-14 5:44 
Hi folks,

I have a simple app to look up products, search box, search button, DataGridView to display results and a loading GIF image to show progress.

Initially, I run the query to display results in the grid successfully, but the GIF image will freeze without refreshing.

I was suggested to use threading to run the query on a different thread, I was able to run the query and the GIF image will refresh Smile | :)

However, when using threading, the scrollbars on the grid will not work, I cannot move them at all Frown | :(

If I remove the threading, scrollbars work, but GIF image will not refresh.

Any pointers on what I am doing wrong here?
See the code below for your reference.

Thanks,
...Alex

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

namespace RmsBottleByLocation
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            System.Environment.Exit(0);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            lblResults.Text = String.Empty;

            grdResults.DataSource = null;
            grdResults.Rows.Clear();
            grdResults.Refresh();

            string keyword = txtKeyword.Text.Trim();
            int minimumKeywordLength = Int32.Parse(ConfigurationManager.AppSettings["MinimumKeywordLength"]);

            if (keyword.Length < minimumKeywordLength)
            {
                MessageBox.Show("Please enter item code or item description with at least " + minimumKeywordLength.ToString() + " characters!",
                    this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                txtKeyword.SelectAll();
                txtKeyword.Focus();

                return;
            }

            loadingImage.Visible = true;
            this.Refresh();

            //ThreadStart threadStart = new ThreadStart(runQuery);
            //Thread queryThread = new Thread(threadStart);
            //queryThread.Start();

            runQuery();
        }

        private void runQuery()
        {
            string keyword = txtKeyword.Text.Trim();

            btnSearch.Enabled = false;
            this.Cursor = Cursors.WaitCursor;

            string hostName = System.Net.Dns.GetHostName();
            string getBottleByLocationStoredProcedure = ConfigurationManager.AppSettings["GetBottleByLocationStoredProcedure"];
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
            string connectionTimeout = ConfigurationManager.AppSettings["ConnectionTimeout"];

            connectionString = string.Format(connectionString, hostName);

            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = getBottleByLocationStoredProcedure;
            command.CommandTimeout = Int32.Parse(connectionTimeout);
            command.Parameters.Add(new SqlParameter("@Keyword", keyword));

            try
            {
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();

                command.Connection = connection;

                SqlDataReader dr = command.ExecuteReader();
                DataTable dt = new DataTable();

                dt.Load(dr);

                grdResults.DataSource = dt;
                grdResults.Columns[0].Width = 500;
                grdResults.Columns[0].Frozen = true;
                grdResults.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

                lblResults.Text = grdResults.Rows.Count.ToString() + " items found!";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                loadingImage.Visible = false;
                this.Refresh();

                btnSearch.Enabled = true;
                this.Cursor = Cursors.Arrow;
            }        
        }

        private void grdResults_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;

            if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && dgv[e.ColumnIndex, e.RowIndex].Value is int)
            {
                if ((int)dgv[e.ColumnIndex, e.RowIndex].Value == 0)
                {
                    e.Value = "-";
                }
            }
        }
    }
}

AnswerRe: Running thread to refresh image disable scrollbars in DataGridView Pin
OriginalGriff26-Apr-14 6:06
mveOriginalGriff26-Apr-14 6:06 
GeneralRe: Running thread to refresh image disable scrollbars in DataGridView Pin
neualex28-Apr-14 3:28
neualex28-Apr-14 3:28 
GeneralRe: Running thread to refresh image disable scrollbars in DataGridView Pin
OriginalGriff28-Apr-14 3:34
mveOriginalGriff28-Apr-14 3:34 
QuestionI got this error on page load my photo gallery page . http://s.codeproject.com/script/Forums/Images/smiley_WTF.gif Pin
300sparta25-Apr-14 23:03
300sparta25-Apr-14 23:03 
AnswerRe: I got this error on page load my photo gallery page . http://s.codeproject.com/script/Forums/Images/smiley_WTF.gif Pin
OriginalGriff25-Apr-14 23:18
mveOriginalGriff25-Apr-14 23:18 
QuestionHow can I change the background color of the part of a ComboBox that is always visible? Pin
arnold_w25-Apr-14 4:34
arnold_w25-Apr-14 4:34 
QuestionRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
Richard Deeming25-Apr-14 4:52
mveRichard Deeming25-Apr-14 4:52 
AnswerRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
arnold_w25-Apr-14 4:56
arnold_w25-Apr-14 4:56 
GeneralRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
Richard Deeming25-Apr-14 5:10
mveRichard Deeming25-Apr-14 5:10 
GeneralRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
Mycroft Holmes25-Apr-14 13:23
professionalMycroft Holmes25-Apr-14 13:23 
GeneralRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
arnold_w27-Apr-14 21:22
arnold_w27-Apr-14 21:22 
GeneralRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
Richard Deeming28-Apr-14 1:39
mveRichard Deeming28-Apr-14 1:39 
GeneralRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
arnold_w28-Apr-14 2:17
arnold_w28-Apr-14 2:17 
GeneralRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
Richard Deeming28-Apr-14 2:32
mveRichard Deeming28-Apr-14 2:32 
GeneralRe: How can I change the background color of the part of a ComboBox that is always visible? Pin
arnold_w28-Apr-14 2:35
arnold_w28-Apr-14 2:35 
QuestionHow can I disallow SelectedValue to be -1 in a ComboBox? Pin
arnold_w25-Apr-14 4:28
arnold_w25-Apr-14 4:28 
AnswerRe: How can I disallow SelectedValue to be -1 in a ComboBox? Pin
OriginalGriff25-Apr-14 4:42
mveOriginalGriff25-Apr-14 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.