none
[C#] 콤보박스와 ListBox, GridView 연결하여 Xml data출력 RRS feed

  • 질문

  • 위와 같은 디자인으로 Xml 분석 프로그램을 만들고 있습니다. 지금 하는일 편하게 하려고 만들고 있는데 너무 힘드네요 ㅠㅠ Xml Data 형식은 다음과 같은 형식입니다.

    <Source> <Item id="22972" subscriptionid="0" name="MULTI_1" mapping="direct" encoding="dec" index="0">0,0,0,0,0,0,0,0,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> <Item id="22972" subscriptionid="0" name="MULTI_2" mapping="direct" encoding="dec" index="0">1,1,1,1,1,1,1,1,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> <Item id="22972" subscriptionid="0" name="MULTI_3" mapping="direct" encoding="dec" index="0">2,2,2,2,2,2,2,2,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> <Item id="22972" subscriptionid="0" name="MULTI_4" mapping="direct" encoding="dec" index="0">3,3,3,3,3,3,3,3,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> </Source>

    Xml 값은 ,로 구분된 여러개의 데이터가 있습니다

    1. 왼쪽 GridView에는 첫번째부터 여덟번째값만 가로가 아닌 세로로 출력 시키고 싶습니다.

    예를들어 콤보박스에서 MULTI_1을 선택하면 왼쪽 GridView에는 다음과 같이 출력될 것입니다.

    data

    1

    0

    2

    0

    3

    0

    4

    0

    5

    0

    6

    0

    7

    0

    8

    0

    2. 오른쪽 GridView에 출력하고 싶은 데이터가 좀 복잡한데요...

    <Source> <Item id="22972" subscriptionid="0" name="MULTI_1" mapping="direct" encoding="dec" index="0">0,0,0,0,0,0,0,0,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> <Item id="22972" subscriptionid="0" name="MULTI_2" mapping="direct" encoding="dec" index="0">1,1,1,1,1,1,1,1,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> <Item id="22972" subscriptionid="0" name="MULTI_3" mapping="direct" encoding="dec" index="0">2,2,2,2,2,2,2,2,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> <Item id="22972" subscriptionid="0" name="MULTI_4" mapping="direct" encoding="dec" index="0">3,3,3,3,3,3,3,3,26065,26125,26160,26195,26230,26270,26310,26350,26390,26430,26470,26510,26545,26580,26660,26680,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,65535,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,</Item> </Source>

    예를들어 콤보박스에서 MULTI_1을 선택하고

    Data[1]

    Data[2]

    Data[3]

    Data[4]

     List박스에서 위 4개의 리스트중 Data[1]을 선택하면 오른쪽 GridView에는 첫번째부터 여덟번째, 아홉번째부터 16번째, 17번째부터 24번째 데이터를 각각 다음과 같이 출력시켜주었음 좋겠습니다.

     1

    2

     Data1

    0

     Data2

    26065 

    26125 

    26160 

    26195 

    26230 

    26270 

    26310 

    26350 

     8

     Data3

     26390

     26430

     26470

     26510

     26545

     26580

    26660 

    26680 

    현재까지 작성한 코드는 다음과 같습니다

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    //using System.Threading.Tasks;
    namespace MLD_manager
    {
        public partial class MLD_manager : Form
        {
            OpenFileDialog ofd = new OpenFileDialog();
            SaveFileDialog sfd = new SaveFileDialog();
            public MLD_manager()
            {
                InitializeComponent();
            }
            private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
            }
            private DataSet ds;
            private void toolStripButton1_Click(object sender, EventArgs e)//임시
            {
                ofd.Filter = "Xml 파일(*.xml)|*.xml|모든파일(*.*)|*.*";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    StreamReader re = new StreamReader(ofd.FileName);
                    textBox1.Text = string.Format("File Path - <{0}>", ofd.FileName);//파일 경로 출력
                    //textBox1.Text = re.ReadToEnd(); //파일 내용 출력
                    ds = new DataSet();
                    ds.ReadXml(re);
                    re.Close();
                    string s = ds.GetXml();
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //-----------------------------콤보 박스에 MULTI 문자가 들어간 item만 출력-------------------------------//
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        //DataRow[] arDR = ds.Tables[0].Select("name like '%" + textBox1.Text.Trim() + "%'");
                        DataRow[] arDR = ds.Tables[0].Select("name like '%" + "MULTI" + "%'");
                        if (arDR.Length > 0)
                        {
                            DataTable dt = new DataTable();
                            dt.Columns.Add("Text", typeof(string));
                            dt.Columns.Add("Value", typeof(string));
                            DataRow dr;
                            for (int i = 0; i < arDR.Length; i++)
                            {
                                dr = dt.NewRow();
                                dr["Value"] = arDR[i]["Id"];
                                dr["Text"] = arDR[i]["name"];
                                dt.Rows.Add(dr);
                            }
                            comboBox1.DisplayMember = "Text";
                            comboBox1.ValueMember = "Value";
                            comboBox1.DataSource = dt;
                        }
                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                        //------------------------------GridView에 콤보박스에서 선택된 데이터 출력-------------------------------//
                            //dataGridView1.DataSource = ds.Tables[0].DefaultView;
                            string[] sArr = Convert.ToString(ds.Tables[0].Rows[0]["NvItem_Text"]).Split(',');
                            DataTable newDT = new DataTable();
                            for (int iCol = 0; iCol < sArr.Length; iCol++)
                            //for (int iCol = 8; iCol < sArr(23); iCol++)
                                newDT.Columns.Add("Col" + (iCol + 1).ToString(), typeof(string));
                            newDT.Rows.Add(sArr);
                            dataGridView1.DataSource = newDT.DefaultView;
                            dataGridView2.DataSource = newDT.DefaultView;
                        
                        //-------------------------------------------------------------------------------------------------------//
                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    }
                    //-------------------------------------------------------------------------------------------------------//
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                }
            }
            private void toolStripButton2_Click(object sender, EventArgs e)//임시
            {
                sfd.Filter = "Xml 파일(*.xml)|*.xml|모든파일(*.*)|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(sfd.FileName);
                    sw.Write(textBox1.Text);
                    sw.Close();
                }
            }
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
            }
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
            }
        }
    }

    실제 xml데이터는 각 MULTI의 name을 가진 하나의 노드에서

     ,로 구분된 데이터 갯수가 1700개가 넘어가서 ㅠㅠ

    일하는데 분석할때마다 앞에서부터 숫자세거나 매번 엑셀열어서 숫자세고 이러고 있느라 너무 오래걸리네요 ㅠㅠ 부탁좀 드릴게요.....ㅠㅠ


    • 편집됨 Byeongha, Kim 2012년 11월 28일 수요일 오전 2:06
    2012년 11월 28일 수요일 오전 2:03

답변

  • 우선 제세해주신 코드를 좀 수정해서 아래와 같이 만들어보았습니다.

    설명이 어려워서 제대로 이해했는지 모르겠네요. 한번 동작을 확인해보시는게 좋을듯합니다.

    코드는 그대로 복사하면 될듯합니다.

    using 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;
    
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            OpenFileDialog ofd = new OpenFileDialog();
            SaveFileDialog sfd = new SaveFileDialog();
    
            public Form1()
            {
                InitializeComponent();
    
                //마지막 BlankRow 안생기도록
                dataGridView1.AllowUserToAddRows = false;
                dataGridView2.AllowUserToAddRows = false;
            }
    
            private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
            }
            private DataSet ds;
            private void toolStripButton1_Click(object sender, EventArgs e)//임시
            {
                ofd.Filter = "Xml 파일(*.xml)|*.xml|모든파일(*.*)|*.*";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    StreamReader re = new StreamReader(ofd.FileName);
                    textBox1.Text = string.Format("File Path - <{0}>", ofd.FileName);//파일 경로 출력
                    //textBox1.Text = re.ReadToEnd(); //파일 내용 출력
                    ds = new DataSet();
                    ds.ReadXml(re);
                    re.Close();
                    string s = ds.GetXml();
    
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //-----------------------------콤보 박스에 MULTI 문자가 들어간 item만 출력-------------------------------//
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        //listBox 초기화
                        listBox1.Items.Clear();
                        listBox1.Items.Add("Data[1]");
                        listBox1.Items.Add("Data[2]");
                        listBox1.Items.Add("Data[3]");
                        listBox1.Items.Add("Data[4]");
    
                        //DataRow[] arDR = ds.Tables[0].Select("name like '%" + textBox1.Text.Trim() + "%'");
                        DataRow[] arDR = ds.Tables[0].Select("name like '%" + "MULTI" + "%'");
                        if (arDR.Length > 0)
                        {
                            DataTable dt = new DataTable();
                            dt.Columns.Add("Text", typeof(string));
                            dt.Columns.Add("Value", typeof(string));
                            DataRow dr;
                            for (int i = 0; i < arDR.Length; i++)
                            {
                                dr = dt.NewRow();
                                dr["Value"] = arDR[i]["name"];
                                dr["Text"] = arDR[i]["name"];
                                dt.Rows.Add(dr);
                            }
                            comboBox1.DisplayMember = "Text";
                            comboBox1.ValueMember = "Value";
                            comboBox1.DataSource = dt;
                        }
                        
                    }
                    //-------------------------------------------------------------------------------------------------------//
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    
                }
            }
            private void toolStripButton2_Click(object sender, EventArgs e)//임시
            {
                sfd.Filter = "Xml 파일(*.xml)|*.xml|모든파일(*.*)|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(sfd.FileName);
                    sw.Write(textBox1.Text);
                    sw.Close();
                }
            }
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //------------------------------GridView에 콤보박스에서 선택된 데이터 출력-------------------------------//
                    DataRow[] arDR = ds.Tables[0].Select("name='" + Convert.ToString(comboBox1.SelectedValue) + "'");
                    if (arDR.Length > 0)
                    {
                        string[] sArr = Convert.ToString(arDR[0]["Item_Text"]).Split(',');
                        //왼쪽 DataGridView
                        if (sArr.Length >= 8)
                        {
                            DataTable newDT = new DataTable();
                            newDT.Columns.Add("Data", typeof(string));
                            DataRow dr8;
                            for (int iCol = 0; iCol < 8; iCol++)
                            {
                                dr8 = newDT.NewRow();
                                dr8["Data"] = Convert.ToString(sArr[iCol]);
                                newDT.Rows.Add(dr8);
                            }
                            dataGridView1.DataSource = newDT.DefaultView;
                            setRowNumber(dataGridView1);
                        }
                        listBox1.SelectedIndex = 0;
                    }
                    //-------------------------------------------------------------------------------------------------------//
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                }
            }
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (listBox1.Items.Count > 0)
                {
                    DataRow[] arDR = ds.Tables[0].Select("name='" + Convert.ToString(comboBox1.SelectedValue) + "'");
                    if (arDR.Length > 0)
                    {
                        int startIdx = 0; int endIdx = 0;
                        switch (Convert.ToString(listBox1.SelectedItem))
                        {
                            case "Data[1]":
                                startIdx = 0;endIdx=7;
                                break;
                            case "Data[2]":
                                startIdx = 8;endIdx=15;
                                break;
                            case "Data[3]":
                                startIdx = 16; endIdx = 23;
                                break;
                            case "Data[4]":
                                startIdx = 24; endIdx = 31;
                                break;
    
                        }
                        string[] sArr = Convert.ToString(arDR[0]["Item_Text"]).Split(',');
                        //왼쪽 DataGridView
                        if (sArr.Length >= endIdx)
                        {
                            DataTable newDT = new DataTable();
                            for (int i = 1; i <= 8; i++)
                                newDT.Columns.Add("Col" + i.ToString(), typeof(string));
    
                            DataRow dr= newDT.NewRow();
                            for (int iCol = startIdx, i=1; iCol <= endIdx; iCol++,i++)
                            {
                                dr["Col"+i.ToString()] = Convert.ToString(sArr[iCol]);
                            }
                            newDT.Rows.Add(dr);
    
                            dataGridView2.DataSource = newDT.DefaultView;
                            setRowNumber(dataGridView2);
                        }
                    }
                }
            }
            private void setRowNumber(DataGridView dgv)
            {
                if (dgv.Rows.Count > 0) dgv.RowHeadersWidth = 70;
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
                }
            }
    
        }
    }

    그럼 즐거운 하루 되세요.


    • 편집됨 GoldrushKoreaMVP 2012년 11월 29일 목요일 오전 8:51
    • 답변으로 제안됨 Sungman KOMVP 2012년 11월 30일 금요일 오전 1:08
    • 답변으로 표시됨 Byeongha, Kim 2012년 11월 30일 금요일 오전 7:20
    2012년 11월 29일 목요일 오전 8:49

모든 응답

  • 우선 제세해주신 코드를 좀 수정해서 아래와 같이 만들어보았습니다.

    설명이 어려워서 제대로 이해했는지 모르겠네요. 한번 동작을 확인해보시는게 좋을듯합니다.

    코드는 그대로 복사하면 될듯합니다.

    using 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;
    
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            OpenFileDialog ofd = new OpenFileDialog();
            SaveFileDialog sfd = new SaveFileDialog();
    
            public Form1()
            {
                InitializeComponent();
    
                //마지막 BlankRow 안생기도록
                dataGridView1.AllowUserToAddRows = false;
                dataGridView2.AllowUserToAddRows = false;
            }
    
            private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
            }
            private DataSet ds;
            private void toolStripButton1_Click(object sender, EventArgs e)//임시
            {
                ofd.Filter = "Xml 파일(*.xml)|*.xml|모든파일(*.*)|*.*";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    StreamReader re = new StreamReader(ofd.FileName);
                    textBox1.Text = string.Format("File Path - <{0}>", ofd.FileName);//파일 경로 출력
                    //textBox1.Text = re.ReadToEnd(); //파일 내용 출력
                    ds = new DataSet();
                    ds.ReadXml(re);
                    re.Close();
                    string s = ds.GetXml();
    
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //-----------------------------콤보 박스에 MULTI 문자가 들어간 item만 출력-------------------------------//
                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        //listBox 초기화
                        listBox1.Items.Clear();
                        listBox1.Items.Add("Data[1]");
                        listBox1.Items.Add("Data[2]");
                        listBox1.Items.Add("Data[3]");
                        listBox1.Items.Add("Data[4]");
    
                        //DataRow[] arDR = ds.Tables[0].Select("name like '%" + textBox1.Text.Trim() + "%'");
                        DataRow[] arDR = ds.Tables[0].Select("name like '%" + "MULTI" + "%'");
                        if (arDR.Length > 0)
                        {
                            DataTable dt = new DataTable();
                            dt.Columns.Add("Text", typeof(string));
                            dt.Columns.Add("Value", typeof(string));
                            DataRow dr;
                            for (int i = 0; i < arDR.Length; i++)
                            {
                                dr = dt.NewRow();
                                dr["Value"] = arDR[i]["name"];
                                dr["Text"] = arDR[i]["name"];
                                dt.Rows.Add(dr);
                            }
                            comboBox1.DisplayMember = "Text";
                            comboBox1.ValueMember = "Value";
                            comboBox1.DataSource = dt;
                        }
                        
                    }
                    //-------------------------------------------------------------------------------------------------------//
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    
                }
            }
            private void toolStripButton2_Click(object sender, EventArgs e)//임시
            {
                sfd.Filter = "Xml 파일(*.xml)|*.xml|모든파일(*.*)|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(sfd.FileName);
                    sw.Write(textBox1.Text);
                    sw.Close();
                }
            }
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //------------------------------GridView에 콤보박스에서 선택된 데이터 출력-------------------------------//
                    DataRow[] arDR = ds.Tables[0].Select("name='" + Convert.ToString(comboBox1.SelectedValue) + "'");
                    if (arDR.Length > 0)
                    {
                        string[] sArr = Convert.ToString(arDR[0]["Item_Text"]).Split(',');
                        //왼쪽 DataGridView
                        if (sArr.Length >= 8)
                        {
                            DataTable newDT = new DataTable();
                            newDT.Columns.Add("Data", typeof(string));
                            DataRow dr8;
                            for (int iCol = 0; iCol < 8; iCol++)
                            {
                                dr8 = newDT.NewRow();
                                dr8["Data"] = Convert.ToString(sArr[iCol]);
                                newDT.Rows.Add(dr8);
                            }
                            dataGridView1.DataSource = newDT.DefaultView;
                            setRowNumber(dataGridView1);
                        }
                        listBox1.SelectedIndex = 0;
                    }
                    //-------------------------------------------------------------------------------------------------------//
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                }
            }
            private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (listBox1.Items.Count > 0)
                {
                    DataRow[] arDR = ds.Tables[0].Select("name='" + Convert.ToString(comboBox1.SelectedValue) + "'");
                    if (arDR.Length > 0)
                    {
                        int startIdx = 0; int endIdx = 0;
                        switch (Convert.ToString(listBox1.SelectedItem))
                        {
                            case "Data[1]":
                                startIdx = 0;endIdx=7;
                                break;
                            case "Data[2]":
                                startIdx = 8;endIdx=15;
                                break;
                            case "Data[3]":
                                startIdx = 16; endIdx = 23;
                                break;
                            case "Data[4]":
                                startIdx = 24; endIdx = 31;
                                break;
    
                        }
                        string[] sArr = Convert.ToString(arDR[0]["Item_Text"]).Split(',');
                        //왼쪽 DataGridView
                        if (sArr.Length >= endIdx)
                        {
                            DataTable newDT = new DataTable();
                            for (int i = 1; i <= 8; i++)
                                newDT.Columns.Add("Col" + i.ToString(), typeof(string));
    
                            DataRow dr= newDT.NewRow();
                            for (int iCol = startIdx, i=1; iCol <= endIdx; iCol++,i++)
                            {
                                dr["Col"+i.ToString()] = Convert.ToString(sArr[iCol]);
                            }
                            newDT.Rows.Add(dr);
    
                            dataGridView2.DataSource = newDT.DefaultView;
                            setRowNumber(dataGridView2);
                        }
                    }
                }
            }
            private void setRowNumber(DataGridView dgv)
            {
                if (dgv.Rows.Count > 0) dgv.RowHeadersWidth = 70;
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    row.HeaderCell.Value = String.Format("{0}", row.Index + 1);
                }
            }
    
        }
    }

    그럼 즐거운 하루 되세요.


    • 편집됨 GoldrushKoreaMVP 2012년 11월 29일 목요일 오전 8:51
    • 답변으로 제안됨 Sungman KOMVP 2012년 11월 30일 금요일 오전 1:08
    • 답변으로 표시됨 Byeongha, Kim 2012년 11월 30일 금요일 오전 7:20
    2012년 11월 29일 목요일 오전 8:49
  • using System.Threading.Tasks;

    에 대해서 질문자님께서 추가질문 주셨었는데...

    VS2008 / Framework 3.5 에서 GoldrushKorea 님의 소스 동작확인 하였습니다. (제 환경)

    해당 참조 삭제하시고, 다시한번 테스트해보세요. ^^

    GoldrushKorea 님께서 윈도우8을 쓰시나봅니다.


    VB/VB.NET is my Life

    2012년 11월 30일 금요일 오전 1:11
  • 답변 감사드려요 ^^

    그냥 VS2010 설치하니까 바로 되네요 ㅠㅠㅋ

    2012년 11월 30일 금요일 오전 5:41
  • 즐거운주말 보내시고,

    또 질문 있으시면 언제든지 글 남겨주세요.

    두번째에있는 GoldrushKorea 님의 스레드 답변으로 등록 부탁드릴게요.


    VB/VB.NET is my Life


    • 편집됨 Sungman KOMVP 2012년 11월 30일 금요일 오전 5:58
    2012년 11월 30일 금요일 오전 5:58
  • 추가질문입니다.

    자세한 답변 감사드립니다~

    오른쪽 GridView에 출력되는 데이터 형태가 알려주신 것 말고 다음과 같이 되었으면 좋겠습니다.

    그림판으로 대충 편집해보았는데요..

    리스트박스에서 하나를 선택하면 한줄이 뜨는것이 아니라

    리스트중 Data[0]을 선택하면 아래에 있는 표처럼 출력되게 하고 싶습니다.

    위에서 작성한 xml을 바탕으로 출력시킨 화면은 다음과 같겠죠?

    리스트박스에 있는 리스트 하나당 저런 데이터 테이블이 생성되게요....

    Data[0]을 리스트박스에서 선택하면

    xml데이터 1번째부터 8번째값은 Data1이라는 열에

    09번째부터 16번째값은 Data2이라는 열에

    17번째부터~24번째값은 Data3이라는 열에 출력 시키는 것이죠..

    리스트박스에서 Data[1]을 선택하면

    25번째 값부터 차례로 Data[0]과 같은 방법으로 GridView에 생성되게 하면 됩니다..






    2012년 11월 30일 금요일 오전 6:12
  • 해결했습니다 ~ㅋ 다들 답변 감사드려요
    2012년 11월 30일 금요일 오전 7:47