Autor Beitrag
neanua
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Mi 09.09.09 09:50 
Ich habe in einem Form eine Liste von Kunden, mit einem Doppelklick soll es möglich sein in eine Detailansicht zu gelangen, mit einem Filter soll man die Auswahl eingrenzen können.

Ich habe ein Beispiel für einen Filter mit DataGrid (nicht DataGridView) gefunden und für meine Interessen umgebaut. Die Filterfunktion funktioniert auch wunderbar.

Die Doppelklickfunktion funktioniert leider nur auf das ungefilterte Grid. Wenn ich das DataGrid filtere und dann doppel klicke ist das Ergebnis so als ob es nicht gefiltert wäre.

Ich denke ich müsste eine andere Tabelle füllen bzw. abfragen, kann mir dabei jemand helfen.

ausblenden volle Höhe C#-Quelltext
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:
        
   public partial class Form5 : Form
    {
        private DataColumnCollection TableColumnCollection;
        private DataTable TableFilterData;

        private DataSet DataSetRecords;
        private DataView ViewRecords;
        string ex_cust_pk="";

        public Form5()
        {
            InitializeComponent();
            LoadTable();
        }


        private void LoadTable()
        {
            try
            {

                DataSetRecords = new DataSet("DataSetRecords");

                string strAccessSelect = @"select 
                                              rec_cust_fk as ID, 
                                              cust_status as Status,
                                              cust_anr as ApoNr,
                                              cust_adr as AdrNr,
                                              cust_name1 as Name1,
                                              cust_name2 as Name2,
                                              cust_strasse as Strasse,
                                              cust_plz as PLZ,
                                              cust_ort as Ort,
                                              cust_tel as Telefon,   
                                              sum(rec_anzahl) as Bestand 
                                       from tb_records 
                                        INNER JOIN tb_Customer 
                                            ON rec_cust_fk =cust_pk
                                        group by rec_cust_fk,
                                                 cust_status,
                                                 cust_anr,
                                                 cust_adr,
                                                 cust_name1,
                                                 cust_name2,
                                                 cust_strasse,
                                                 cust_plz,
                                                 cust_ort,
                                                 cust_tel"
;

                string connectionString = "Data Source=" + IP + "," + PORT + ";User ID=" + USERNAME + ";Password=" +               PASSWORD + ";Provider = SQLOLEDB" + ";Initial Catalog=" + DB + ";Trusted_Connection=True" + ";";
                OleDbConnection myConnection = new OleDbConnection(connectionString);
                OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myConnection);
                OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
                myConnection.Open();
                myDataAdapter.Fill(DataSetRecords, "TempAbfrage");
                DataRowCollection dra = DataSetRecords.Tables["TempAbfrage"].Rows;
                myConnection.Close();

                TableColumnCollection = DataSetRecords.Tables[0].Columns;

                ViewRecords = DataSetRecords.Tables[0].DefaultView;

                DataGridTableStyle GridStyle = new DataGridTableStyle();
                GridStyle.MappingName = DataSetRecords.Tables[0].TableName;
                //GridStyle.BackColor = System.Drawing.Color.Gray;
                GridStyle.AlternatingBackColor = System.Drawing.Color.AliceBlue;               
                GridStyle.GridLineColor = System.Drawing.Color.MediumSlateBlue;


                DataGridRecords.TableStyles.Add(GridStyle);
                DataGridRecords.SetDataBinding(ViewRecords, "");

            }
            catch (System.Exception a_Ex)
            {
                MessageBox.Show(a_Ex.Message);
            }
        }


        

        private void ButtonDataFilter_Click(object sender, EventArgs e)
        {
            DataFilterForm DataFilter = new DataFilterForm();
            DataFilter.SetSourceColumns(TableColumnCollection);
            DataFilter.ShowDialog();
            TableFilterData = DataFilter.GetFilterDataTable();
            SetTableByDataFilter();
        }

        private void SetTableByDataFilter()
        {

            ViewRecords = new DataView(DataSetRecords.Tables[0]);

            try
            {
                // Build the RowFilter statement according to the user restriction 
                foreach (DataRow FilterRow in TableFilterData.Rows)
                {

                    if (FilterRow["Operation"].ToString() != string.Empty && FilterRow["ColumnData"].ToString() != string.Empty)
                    {
                        // Add the "AND" operator only from the second filter condition 
                        // The RowFilter get statement which simallar to the Where condition in sql query
                        // For example "GroupID = '6' AND GroupName LIKE 'A%' 
                        if (ViewRecords.RowFilter == string.Empty)
                        {
                            ViewRecords.RowFilter = FilterRow["ColumnName"].ToString() + " " + FilterRow["Operation"].ToString() + " '" + FilterRow["ColumnData"].ToString() + "' ";
                        }

                        else
                        {
                            ViewRecords.RowFilter += " AND " + FilterRow["ColumnName"].ToString() + " " + FilterRow["Operation"].ToString() + " '" + FilterRow["ColumnData"].ToString() + "'";
                        }

                    }
                }

                DataGridRecords.SetDataBinding(ViewRecords, "");
            }

            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void DataGridRecords_DoubleClick(object sender, EventArgs e)
        {
            string wert = ViewRecords.Table.Rows[DataGridRecords.CurrentRowIndex].ItemArray[0].ToString();
            Form6 xForm = new Form6();
            ex_cust_pk = wert;
            xForm.TextToShow = ex_cust_pk;
            xForm.Show();         
        }
    }
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 09.09.09 18:12 
:welcome:

Geh nicht über CurrentRowIndex, sondern über CurrentRow.DataBoundItem. Das sollte dann eine DataRowView-Instanz zurückgeben.

_________________
>λ=
neanua Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 10.09.09 09:38 
string wert = ViewRecords.Table.Rows[DataGridRecords.CurrentRowIndex].ItemArray[0].ToString();


DataGridRecords.CurrentRow gibt es leider nicht bei DataGrid, oder habe ich etwas falsch verstanden.

Danke für das Welcome.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Do 10.09.09 18:30 
Ups. Ich habe nur gelesen, dass das Beispiel für DataGrid war, nicht, dass du es selbst benutzt. Warum willst du denn nicht das neuere DataGridView einsetzen?

_________________
>λ=
neanua Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Fr 11.09.09 09:11 
Ganz einfach, weil ich mit dem DataGridView den Filter nicht hinbekommen habe.

Ein Teufelskreis. :(
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 11.09.09 18:21 
Über eine DataView sollte das genauso im DataGridView funktionieren :nixweiss: .

_________________
>λ=