Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGrid Desktop
I 'm pretty new to c# and try to look for a solution to create a little first app with excel.
 
What I try to do is to make a order list and later I want to export to PDF.
 
Step 1 scan a barcode and post to first column in data grid
Step 2 search the barcode in excel and post back the description into the datagrid column 3
Step 3 enter the Qty en jump back to txt box 1
 

 
sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void orderArtikelNummerTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {                
//Move to Qty
                orderQtyTextBox.Focus();
                e.Handled = true;
                orderQtyTextBox.Select(0, orderQtyTextBox.Text.Length);
 
            }
        }
 
        private void orderQtyTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
// check excell for valeu and post to column 3 !!!TODO!!!
//Post textboxes in grid and create new line
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(this.orderDataGridView, productNameTextBox.Text, orderQtyTextBox.Text);
                this.orderDataGridView.Rows.Add(row);
                
//Move to Artikelnummer
                productNameTextBox.Focus();
                e.Handled = true;
                productNameTextBox.Select(0, productNameTextBox.Text.Length);
            }
        }
 

example of excel:
 
(A1)12345 (B1) some description of the product
 
Any suggestions?
I search 't google codeproject etc.. but can 't find any readable solution
 
thnks
Posted 21-Jan-13 11:10am
Edited 21-Jan-13 11:19am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

private Microsoft.Office.Interop.Excel.Range GetSpecifiedRange(string matchStr, Microsoft.Office.Interop.Excel.Worksheet objWs)
        {
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Range currentFind = null;
            //Microsoft.Office.Interop.Excel.Range firstFind = null;
            currentFind = objWs.get_Range("A1", "AM100").Find(matchStr, missing,
                           Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues,
                           Microsoft.Office.Interop.Excel.XlLookAt.xlPart,
                           Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
                           Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, missing, missing);
            return currentFind;
        }
 
        private void orderArtikelNummerTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                //post data into 
               
               //Move to Qty
                orderQtyTextBox.Focus();
                e.Handled = true;
                orderQtyTextBox.Select(0, orderQtyTextBox.Text.Length);
            }
        }
        private void orderQtyTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                string File_name = "E:\\catalog.xls";
                Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook oWB;
                Microsoft.Office.Interop.Excel.Worksheet oSheet;
                try
                {
                    //Move to Artikelnummer
                    orderArtikelNummerTextBox.Focus();
                    e.Handled = true;
                    orderArtikelNummerTextBox.Select(0, orderArtikelNummerTextBox.Text.Length);
                  
                    //start search excell
                    object missing = System.Reflection.Missing.Value;
                    oWB = oXL.Workbooks.Open(File_name, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing);
                    oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets[1];
                    Microsoft.Office.Interop.Excel.Range oRng = GetSpecifiedRange(orderArtikelNummerTextBox.SelectedText, oSheet);
                    if (oRng != null)
                    {
                        MessageBox.Show("Text found, position is Row:" + oRng.Row + " and column:" + oRng.Column);
                        
                        DataGridViewRow row = new DataGridViewRow();
                        row.CreateCells(this.orderDataGridView, orderArtikelNummerTextBox.Text, orderQtyTextBox.Text);
                        this.orderDataGridView.Rows.Add(row); 
                        
                        
                        //orderDataGridView.Rows[0].Cells[0].Value = orderArtikelNummerTextBox.Text;
                        //orderDataGridView.Rows[+1].Cells[0 + 2].Value = orderQtyTextBox.Text;
                                            }
                    else
                    {
                        MessageBox.Show("Text is not found"); 
                    }
                    oWB.Close(false, missing, missing);
                    oSheet = null;
                    oWB = null;
                    oXL.Quit();
                }
                catch (Exception ex)
                {
                    {
                        MessageBox.Show(" Exception caught." + ex);
                    }
                }
            }
        }
 
Okay found how to read excel and get row and column number
 
Any suggestion how to count with the column number, Lets say +1 and than ask to excell the new counted column?
 
kind regards
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 505
1 OriginalGriff 248
2 ChintanShukla 185
3 Richard Deeming 175
4 Bhavik_Patel 170
0 Sergey Alexandrovich Kryukov 8,688
1 OriginalGriff 7,237
2 CPallini 2,598
3 Richard MacCutchan 2,080
4 Abhinav S 1,888


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100