Question : VS 2008 C# DataGridViewButtonColumn fires twice

I added a lookup button to my datagridview by adding a datagridviewButtonCoulumn. I click the lookup button. the lookup screen appears, the double click event brings the value back, the lookup screen is supposed to go away but the problem I have is that the lookup window fires again and locks the screen. What is making the lookup screen fire again?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
namespace PopulateJobLinkerRTE
{
    public partial class frmViewJobLinker : DexUIForm
    {

        /* Keep a reference to the View Agencies By Jobs Form WinForm */
        static frmLookupJob ViewAgenciesByJobsForm;

        /* Create a reference to the Receivables Transaction Entry screen */
        static RmSalesEntryForm RmSalesEntryForm = Dynamics.Forms.RmSalesEntry;
        static RmSalesEntryForm.RmSalesEntryWindow RmSalesEntryWindow = RmSalesEntryForm.RmSalesEntry;
        
        /* Create a reference to the Receivables Transaction Entry Distribution screen */
        static RmSalesDistributionForm RmSalesDistributionForm = Dynamics.Forms.RmSalesDistribution;
        static RmSalesDistributionForm.RmSalesDistributionWindow RmSalesDistributionWindow = RmSalesDistributionForm.RmSalesDistribution;

        /* Define a string variable to hold connection string */
        string ConnectionString;
                       
        /* Set up SQL Connection Handlers */
        private System.Data.SqlClient.SqlConnection DataConnection;
        private System.Data.DataSet DataDataSet;
        private System.Data.SqlClient.SqlCommand DataCommand;
        private System.Data.SqlClient.SqlDataAdapter DataDataAdapter;
        private System.Data.DataSet TrxTypeDataSet;
        private System.Data.SqlClient.SqlCommand TrxTypeDataCommand;
        private System.Data.SqlClient.SqlDataAdapter TrxTypeDataAdapter;
        private SqlCommandBuilder _commandBuilder;

        /* Define variables to hold batch number and batch source */
        string DocumentNumber;
        string MasterType;
        string DistSource;
        string DocSource;
        
        public frmViewJobLinker()
        {
            InitializeComponent();
                        
            txtCustomerID.Text = RmSalesEntryWindow.CustomerNumber;
            txtCustomerName.Text = RmSalesEntryWindow.CustomerName;
            txtDocumentNumber.Text = RmSalesEntryWindow.RmDocumentNumberWork;
            DocumentNumber = RmSalesEntryWindow.RmDocumentNumberWork;
            
            switch (RmSalesEntryWindow.DocumentType.Value.ToString())
            {
                case "1":
                    txtDocumentType.Text = "Sales/Invoices";
                    break;
                case "2":
                    txtDocumentType.Text = "Debit Memos";
                    break;
                case "3":
                    txtDocumentType.Text = "Finance Charges";
                    break;
                case "4":
                    txtDocumentType.Text = "Service/Repairs";
                    break;
                case "5":
                    txtDocumentType.Text = "Warranty";
                    break;
                case "6":
                    txtDocumentType.Text = "Credit Memo";
                    break;
                case "7":
                    txtDocumentType.Text = "Returns";
                    break;
                default:
                    txtDocumentType.Text = "Unknown";
                    break;
            }


            txtDocumentAmount.Text = RmSalesEntryWindow.DocumentAmount.Value.ToString("c");
            DocumentNumber = RmSalesEntryWindow.RmDocumentNumberWork;

            txtBatchName.Text = RmSalesEntryWindow.BatchNumber;
            /* Convert the posting date */
            txtPostingDate.Text = RmSalesEntryWindow.GlPostingDate.Value.ToString();
            try
            {

                if (txtPostingDate.Text != "")
                {
                    txtPostingDate.Text = Convert.ToDateTime(txtPostingDate.Text).ToString("MM/dd/yyyy");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR::" + ex.Message);
                this.Close();

            }
            /* Convert the document date */
            txtDocumentDate.Text = RmSalesEntryWindow.DocumentDate.Value.ToString();
            try
            {
                if (txtDocumentDate.Text.ToString() != "")
                {
                    txtDocumentDate.Text = Convert.ToDateTime(txtDocumentDate.Text).ToString("MM/dd/yyyy");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR::" + ex.Message);
                this.Close();

            }


            MasterType = "CUS";
            DistSource = "Receivables Transaction Entry";
            DocSource = "Receivables";

            /* Populate the Job Linker table based on this Document Number */
            try
            {
                SqlConnection con = GetConnection();
                SqlCommand cmd = new SqlCommand();

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@DCMTNMBR", DocumentNumber));
                cmd.Parameters.Add(new SqlParameter("@MASTERTYPE", MasterType));
                cmd.Parameters.Add(new SqlParameter("@DISTSOURCE", DistSource));
                cmd.Parameters.Add(new SqlParameter("@DOCSOURCE", DocSource));

                cmd.CommandText = "rbsUpdateJobLinkerRTE";

                cmd.Connection = con;

                cmd.ExecuteNonQuery();

                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR::" + ex.Message);
                this.Close();

            }

            /* Display the GL Distributions from the Job Linker table per Document Number */
            try
            {
                SqlConnection DataConnection = GetConnection();

                /* Define dataset for Transaction Types */
                TrxTypeDataSet = new System.Data.DataSet();
                TrxTypeDataSet.CaseSensitive = false;

                TrxTypeDataCommand = new System.Data.SqlClient.SqlCommand();
                TrxTypeDataCommand.Connection = DataConnection;

                TrxTypeDataCommand.CommandText = "select LTRIM(DISTTRXTYPE) AS DISTTRXTYPE from JOB_TRX_TYPES where formtype='SALES' ORDER BY DISTTRXTYPE";

                TrxTypeDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                TrxTypeDataAdapter.SelectCommand = TrxTypeDataCommand;

                TrxTypeDataAdapter.Fill(TrxTypeDataSet);

                /* Define dataset for GL distribution amounts */
                DataDataSet = new System.Data.DataSet();
                DataDataSet.CaseSensitive = false;

                DataCommand = new System.Data.SqlClient.SqlCommand();
                DataCommand.Connection = DataConnection;

                DataCommand.CommandText = "select JOBNUMBER,ACTNUMST,DEBITAMT,CRDTAMNT,DISTTYPE,DISTTRXTYPE,DISTREF,BCHSOURC,TRANSNMBR,TRXTYPE,DSTSQNUM,CNTRLTYP,APTVCHNM,SPCLDIST " +
                "from JOB_LINKER WHERE TRANSNMBR ='" + DocumentNumber + "' order by DSTSQNUM";

                DataDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
                DataDataAdapter.SelectCommand = DataCommand;
                _commandBuilder = new SqlCommandBuilder(DataDataAdapter);

                DataDataAdapter.Fill(DataDataSet);

                dgvViewJobLinker.DataError += new DataGridViewDataErrorEventHandler(dgvJobLinker_DataError);

                dgvViewJobLinker.RowHeadersVisible = false;
                dgvViewJobLinker.DataSource = DataDataSet.Tables[0].DefaultView;
                dgvViewJobLinker.AutoGenerateColumns = true;
                dgvViewJobLinker.DefaultCellStyle.NullValue = ' ';
                dgvViewJobLinker.AllowUserToAddRows = false;

                DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
                colButton.HeaderText = "";
                colButton.Name = "btnLookupJob";
                colButton.Text = "Lookup Job";
                colButton.UseColumnTextForButtonValue = true;

                dgvViewJobLinker.Columns.Insert(0, colButton);
                dgvViewJobLinker.CellContentClick += new DataGridViewCellEventHandler(dgvViewJobLinker_CellContentClick);

                dgvViewJobLinker.Columns[0].Width = 80;
               
                dgvViewJobLinker.Columns[1].Width = 100;
                dgvViewJobLinker.Columns[1].ReadOnly = false;
                dgvViewJobLinker.Columns[1].HeaderText = "Job Number";

                dgvViewJobLinker.Columns[2].Width = 100;
                dgvViewJobLinker.Columns[2].ReadOnly = true;
                dgvViewJobLinker.Columns[2].HeaderText = "GL Account";

                dgvViewJobLinker.Columns[3].Width = 84;
                dgvViewJobLinker.Columns[3].ReadOnly = true;
                dgvViewJobLinker.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvViewJobLinker.Columns[3].DefaultCellStyle.Format = "c";
                dgvViewJobLinker.Columns[3].HeaderText = "Debit Amount";

                dgvViewJobLinker.Columns[4].Width = 84;
                dgvViewJobLinker.Columns[4].ReadOnly = true;
                dgvViewJobLinker.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                dgvViewJobLinker.Columns[4].DefaultCellStyle.Format = "c";
                dgvViewJobLinker.Columns[4].HeaderText = "Credit Amount";

                dgvViewJobLinker.Columns[5].Width = 100;
                dgvViewJobLinker.Columns[5].ReadOnly = true;
                dgvViewJobLinker.Columns[5].HeaderText = "Dist Type";

                /* Add a new Column (ComboBox) for the Transaction Type column */
                DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();

                colType.DefaultCellStyle.NullValue = " ";
                colType.DataSource = TrxTypeDataSet.Tables[0];
                colType.ValueMember = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                colType.DisplayMember = colType.ValueMember;
                colType.HeaderText = "Transaction Type";
                colType.DropDownWidth = 90;
                colType.Width = 90;
                colType.MaxDropDownItems = 7;
                colType.DataPropertyName = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                colType.Name = DataDataSet.Tables[0].Columns[5].ColumnName.ToString();
                dgvViewJobLinker.Columns.RemoveAt(6);
                dgvViewJobLinker.Columns.Insert(6, colType);

                dgvViewJobLinker.Columns[7].Width = 292;
                dgvViewJobLinker.Columns[7].ReadOnly = false;
                dgvViewJobLinker.Columns[7].HeaderText = "Dist Reference";

                dgvViewJobLinker.Columns[8].Visible = false;
                dgvViewJobLinker.Columns[9].Visible = false;
                dgvViewJobLinker.Columns[10].Visible = false;
                dgvViewJobLinker.Columns[11].Visible = false;
                dgvViewJobLinker.Columns[12].Visible = false;
                dgvViewJobLinker.Columns[13].Visible = false;
                dgvViewJobLinker.Columns[14].Visible = false;

                dgvViewJobLinker.EditMode = DataGridViewEditMode.EditOnEnter;
                dgvViewJobLinker.Focus();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }

        private SqlConnection GetConnection()
        {
            /* Create reader and open file containing connection string */
            TextReader tr = new StreamReader(@"C:\Program Files\Microsoft Dynamics\GP\Addins\GSE.INI");

            /* Read line containing the connection string */
            ConnectionString = tr.ReadLine();

            /* Close the stream */
            tr.Close();

            DataConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);
            DataConnection.Open();

            return DataConnection;
        }

        private void dgvViewJobLinker_SelectionChanged(object sender, EventArgs e)
        {

        }



        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //DataCommandBuilder = new SqlCommandBuilder(DataDataAdapter);
                //DataDataAdapter.DeleteCommand = DataCommandBuilder.GetDeleteCommand();
                //DataDataAdapter.InsertCommand = DataCommandBuilder.GetInsertCommand();
                //DataDataAdapter.UpdateCommand = DataCommandBuilder.GetUpdateCommand();
                _commandBuilder.GetUpdateCommand();

                DataDataAdapter.Update(DataDataSet);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Hide();
            this.Dispose();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dgvJobLinker_DataError(object sender, DataGridViewDataErrorEventArgs anError)
        {
            
        }

        private void dgvViewJobLinker_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dgvViewJobLinker_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > 0)
            {

                if (ViewAgenciesByJobsForm == null)
                {
                    ViewAgenciesByJobsForm = new frmLookupJob();
                }
                else
                {
                    if (ViewAgenciesByJobsForm.Created == false)
                    {
                        ViewAgenciesByJobsForm = new frmLookupJob();
                    }
                }

                ViewAgenciesByJobsForm.ShowDialog();
                /* ViewAgenciesByJobsForm.Show(); 
                ViewAgenciesByJobsForm.Activate(); */
                dgvViewJobLinker[1, dgvViewJobLinker.CurrentCell.RowIndex].Value = ViewAgenciesByJobsForm.CellValue;

            }
        }
        }
    }

Answer : VS 2008 C# DataGridViewButtonColumn fires twice

Hi,
  it is best practice to use "using" to create,show and destroy the modal forms. you should check for whether user is changed something and clicked accept button (which a button sets DialogResult = DialogResult.OK).if he click "cancel" button (which a button sets DialogResult = DialogResult.Cancel), you should not do update cell value. have attached sample.

ViewAgenciesByJobsForm.ShowDialog(this)

sets current form as parent form.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
if (e.ColumnIndex == 0 && e.RowIndex > 0)
{
  using  (frmLookupJob ViewAgenciesByJobsForm =
                       ViewAgenciesByJobsForm = new frmLookupJob())
  {
     if(ViewAgenciesByJobsForm.ShowDialog(this) == DialogResult.OK))
         dgvViewJobLinker[1,dgvViewJobLinker.CurrentCell.RowIndex].Value                   = ViewAgenciesByJobsForm.CellValue;

   }
}


in frmLookupJob , it will something like:

(1) btnOk click event :
    CellValue = "my new Value"
    DialogResult   =DialogResut.OK;

(2) btnCancel click event :
    DialogResult   =DialogResut.Cancel;
Random Solutions  
 
programming4us programming4us