Click here to Skip to main content
15,906,574 members
Home / Discussions / C#
   

C#

 
GeneralRe: Multiple forms and datagridviews Pin
bwood202026-May-09 6:13
bwood202026-May-09 6:13 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute26-May-09 6:20
Henry Minute26-May-09 6:20 
GeneralRe: Multiple forms and datagridviews Pin
bwood202026-May-09 12:47
bwood202026-May-09 12:47 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute26-May-09 13:11
Henry Minute26-May-09 13:11 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute26-May-09 13:18
Henry Minute26-May-09 13:18 
GeneralRe: Multiple forms and datagridviews [modified] Pin
bwood202027-May-09 5:44
bwood202027-May-09 5:44 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute27-May-09 7:20
Henry Minute27-May-09 7:20 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute27-May-09 10:41
Henry Minute27-May-09 10:41 
Just when you thought it was safe to ......... Big Grin | :-D

Some mods to the first form, I am posting the whole thing, because there are some renames, as well as some new methods and fields:
    List<string> standardList = new List<string>();
    Dictionary<string, string> clientHeaders = new Dictionary<string, string>();
    Dictionary<string, string> standardHeaders = new Dictionary<string, string>();
    DataSet testDataSet = null;
    DataTable headerTable = null;
    DataTable clientDataTable = null;
    DataTable standardDataTable = null;
    MappingForm mappingForm = null;
    readonly string dataFile = "TestData.csv";

    FieldMappingDictionary docsDictionary = null;

    public ListEditorForm()
    {
        InitializeComponent();
        InitStandardList();
    }

    private void InitStandardList()
    {
        standardList.AddRange(new string[] { "Property", "Offer", "Documentation", "Figures", "Guarantee" });
    }

    private void ProcessMods()
    {
        // Create a copy of original table structure with
        // standardized column names**
        // load original file data into it
        this.GetClientData();
        this.CreateStandardTable();

        // then do a series of updates using the docsDictionary, and the
        // other Dictionaries
        // STEP THROUGH THIS TO WATCH UPDATE STATEMENT BUILDING
        //StringBuilder selectBuilder = null;
        foreach (string key in this.docsDictionary.Keys)
        {
            foreach (string val in this.docsDictionary[key])
            {
                //selectBuilder = new StringBuilder();
                //selectBuilder.Append("UPDATE <yourtablenamehere**> SET Documentation = ");
                //selectBuilder.Append(key);
                //selectBuilder.Append(" WHERE Documentation = ");
                //selectBuilder.Append(val);

                //string updateQuery = selectBuilder.ToString();

                // execute the query here
                // you know OleDbConnection, OleDbcommand etc.
                // then go round for next mod
                foreach (DataRow row in this.standardDataTable.Rows)
                {
                    if (row["Documentation"].ToString() == val)
                    {
                        row["Documentation"] = key;
                    }
                }
            }
            // go round for next key
        }

        // then do same for next dictionary
    }

    private void MakeHeaderDictionaries()
    {
        this.clientHeaders = this.MakeHeaderDictionary();
        this.standardHeaders = this.MakeStandardHeaderDictionary();
    }

    private Dictionary<string, string> MakeStandardHeaderDictionary()
    {
        Dictionary<string, string> result = new Dictionary<string, string>();
        foreach (DataRow row in this.headerTable.Rows)
        {
            result.Add(row[0].ToString().Trim(), row[1].ToString().Trim());
        }

        return result;
    }

    private Dictionary<string, string> MakeHeaderDictionary()
    {
        Dictionary<string, string> result = new Dictionary<string, string>();
        foreach (DataRow row in this.headerTable.Rows)
        {
            result.Add(row[1].ToString().Trim(), row[0].ToString().Trim());
        }

        return result;
    }

    private void GetClientData()
    {
        //Connect to csv file
        String comString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
            Path.GetDirectoryName(Path.GetFullPath(this.dataFile)) + @";Extended Properties=""Text;HDR=YES;FMT=Delimited;IMEX=1\""";

        String strSql = "SELECT * FROM [" + Path.GetFileName(this.dataFile) + "]";
        using (OleDbConnection conCSV = new OleDbConnection(comString))
        {
            conCSV.Open();
            OleDbCommand dbCommand = new OleDbCommand(strSql, conCSV);
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(dbCommand);
            clientDataTable = new DataTable("ClientData");
            dAdapter.Fill(clientDataTable);

            this.testDataSet.Tables.Add(clientDataTable);
        }
    }

    private void CreateStandardTable()
    {
        this.standardDataTable = this.clientDataTable.Copy();
        this.standardDataTable.TableName = "StandardData";
        for (int i = 0; i < this.standardDataTable.Columns.Count; i++)
        {
            this.standardDataTable.Columns[i].ColumnName =
                this.standardHeaders[this.clientDataTable.Columns[i].ColumnName.Trim()];
        }

        this.testDataSet.Tables.Add(standardDataTable);
    }

    #region ListEditorForm PROPERTIES ...............................
    public Dictionary<string, string> ClientHeaders
    {
        get
        {
            return this.clientHeaders;
        }

        private set
        {
            this.clientHeaders = value;
        }
    }

    public Dictionary<string, string> StandardHeaders
    {
        get
        {
            return this.standardHeaders;
        }

        private set
        {
            this.standardHeaders = value;
        }
    }

    private MappingForm MappingForm
    {
        get
        {
            if (this.mappingForm == null)
            {
                this.mappingForm = new MappingForm();
            }

            return this.mappingForm;
        }
    }
    #endregion ListEditorForm PROPERTIES

    private void ListEditorForm_Load(object sender, EventArgs e)
    {
        string headerLine;
        using (StreamReader sr = new StreamReader(dataFile))
        {
            headerLine = sr.ReadLine();
        }

        testDataSet = new DataSet("HeaderDataSet");
        headerTable = new DataTable("ClientHeaders");
        DataColumn newColumn = new DataColumn("ClientHeader", typeof(string));
        headerTable.Columns.Add(newColumn);
        newColumn = new DataColumn("StandardHeader", typeof(string));
        headerTable.Columns.Add(newColumn);

        DataRow newRow = null;
        foreach (string s in headerLine.Split(','))
        {
            newRow = headerTable.NewRow();
            newRow[0] = s;
            newRow[1] = "Property";
            headerTable.Rows.Add(newRow);
        }

        testDataSet.Tables.Add(headerTable);

        DataGridViewComboBoxColumn cbCol =
            this.dataGridView1.Columns["standardColumn"] as DataGridViewComboBoxColumn;
        cbCol.DataSource = this.standardList;

        this.dataGridView1.AutoGenerateColumns = false;
        this.dataGridView1.Columns[0].DataPropertyName = "ClientHeader";
        this.dataGridView1.Columns[1].DataPropertyName = "StandardHeader";
        this.dataGridView1.DataSource = testDataSet;
        this.dataGridView1.DataMember = "ClientHeaders";
    }

    private void btnGetValue_Click(object sender, EventArgs e)
    {
        this.MakeHeaderDictionaries();
        this.MappingForm.HeaderDictionary = this.clientHeaders;
        this.MappingForm.DataFile = this.dataFile;
        this.MappingForm.FormClosing += new FormClosingEventHandler(MappingForm_Closed);
        this.MappingForm.Show();
    }

    void MappingForm_Closed(object sender, FormClosingEventArgs e)
    {
        this.docsDictionary = this.MappingForm.DocumentsDictionary;
        // get rest of dictionaries here

        this.ProcessMods();

        this.testDataSet.Tables["StandardData"].WriteXml("TestData.xml", XmlWriteMode.WriteSchema);
    }
}


There are no changes to MappingForm, at this time.
TestData.xml will be in the debug directory. When examining it you must remember that the code, as is, only corrects the 'Documentation' column.

I also found out why the FieldMappingDictionary gave you errors the first time, here is the corrected version:
public class FieldMappingDictionary : Dictionary<string, List<string>>
{
    public void Add(string key, string value)
    {
        // if the key is already in the Dictionary
        if (base.ContainsKey(key))
        {
            // only do it if value not already there
            if (!base[key].Contains(value))
            {
                // add the new value
                base[key].Add(value);
            }
        }
        else
        {
            // if the key ain't there, add it in a new values list
            List<string> list = new List<string>();
            list.Add(value);
            base.Add(key, list);
        }
    }
}


Let me know what you think. Be gentle with me. Smile | :)

Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

GeneralRe: Multiple forms and datagridviews Pin
Henry Minute27-May-09 11:39
Henry Minute27-May-09 11:39 
GeneralRe: Multiple forms and datagridviews Pin
bwood202027-May-09 12:18
bwood202027-May-09 12:18 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute27-May-09 12:29
Henry Minute27-May-09 12:29 
GeneralRe: Multiple forms and datagridviews Pin
Henry Minute27-May-09 12:35
Henry Minute27-May-09 12:35 
GeneralRe: Multiple forms and datagridviews Pin
bwood202027-May-09 13:02
bwood202027-May-09 13:02 
GeneralRe: Multiple forms and datagridviews Pin
bwood202029-May-09 5:27
bwood202029-May-09 5:27 
GeneralRe: Multiple forms and datagridviews Pin
bwood202018-Jun-09 12:06
bwood202018-Jun-09 12:06 
QuestionCheck if frame in webclient is done loading Pin
Member 441789215-May-09 4:25
Member 441789215-May-09 4:25 
Questionposting data to remote URL using WebBrowser Pin
shabya15-May-09 2:49
shabya15-May-09 2:49 
QuestionConvert EBCDIC to ASCII and vice versa Pin
deep715-May-09 2:15
deep715-May-09 2:15 
AnswerRe: Convert EBCDIC to ASCII and vice versa Pin
OriginalGriff15-May-09 2:23
mveOriginalGriff15-May-09 2:23 
GeneralRe: Convert EBCDIC to ASCII and vice versa Pin
deep715-May-09 2:39
deep715-May-09 2:39 
GeneralRe: Convert EBCDIC to ASCII and vice versa Pin
Alan Balkany15-May-09 3:31
Alan Balkany15-May-09 3:31 
AnswerRe: Convert EBCDIC to ASCII and vice versa Pin
Scott Barbour15-May-09 9:59
Scott Barbour15-May-09 9:59 
GeneralRe: Convert EBCDIC to ASCII and vice versa Pin
ishtine188-Jan-10 19:04
ishtine188-Jan-10 19:04 
GeneralRe: Convert EBCDIC to ASCII and vice versa Pin
frank_a3208-Feb-11 11:41
frank_a3208-Feb-11 11:41 
QuestionDatetime picker Pin
KIDYA15-May-09 2:07
KIDYA15-May-09 2:07 

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.