c# - 填充 DataGrid : how to manipulate data before showing it on C# MySQL

标签 c# mysql wpf visual-studio datagrid

首先,我是 VS2015+C#+XAML 的新手,我来自 VB6,所以我习惯以某种方式做事,我想弄清楚如何在 VS2015 中做同样的事情。

我需要从 MySQL 数据库填充我的 WPF 应用程序中的 DataGrid,因此我发现有两种方法可以做到这一点:

1) 手动使用:

DataGrid.Items.Add(MyCustomClass)

2) 填充它

DataGrid.ItemsSource = DataTable.DefaultView;

我认为最好的方法是使用第二种方法,它速度更快,而且我不需要为每个要显示的结果集创建自定义类。在我开始尝试在 VB6 中做一些我用来做的事情之前,它工作得很好

为了方便起见,我将数据库中的日期保存为数字而不是日期,因此当我检索日期时,我会在显示它之前对其进行格式化,其他类型的数据也是如此。

但是当我在 C# 中执行此操作时,我无法在显示数据之前对其进行操作。

主要问题:

1) 当试图显示串联的 SQL 字段时,它显示字节 [] 数组而不是实际数据。

2) 不能在显示之前操纵日期。

我的 XAML 代码

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <TextBlock Text="oViewer" FontSize="26.667" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="10" Margin="20,0,0,0" Foreground="Black" Cursor="None"/>
    <StackPanel Orientation="Vertical" Grid.Row="1">
        <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0" Padding="0">
            <StackPanel Margin="10,10,10,10">
                <Button Content="Fill Data" Click="Button_Click"/>
                <TextBlock Text="Customers:"/>
                <DataGrid x:Name="G" Height="160" Margin="0,5,0,10" RowHeight="40" AutoGenerateColumns="False" AlternatingRowBackground="Gainsboro" >
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Code" Width="SizeToCells" Binding="{Binding CODE}" FontSize="11" />
                        <DataGridTextColumn Header="Name" Width="SizeToCells" Binding="{Binding NAME}" FontSize="11" />
                        <DataGridTextColumn Header="Date" Width="SizeToCells" Binding="{Binding EDATE,StringFormat=\{0:dd-MMM-yyyy\}}" FontSize="11" />
                    </DataGrid.Columns>

                </DataGrid>
            </StackPanel>
        </ScrollViewer>
    </StackPanel>

</Grid>

如您所见,我在日期方面做了一些尝试,但没有取得任何成果。

我的 C# 代码

public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {

        DB DB = new DB(); //MYSQL CONNECTION CLASS
        string SQL = "SELECT NAME, DATE, CONCAT(LETTER,NUMBER) AS CODE";
        DataTable RS = DB.Select(SQL); //DB.SELECT RETURN A DATATABLE WITH RESULT SET

        G.ItemsSource = RS.DefaultView; //POPULATING THE DATAGRID


    }

希望你能帮我解决这个问题。预先感谢您的回答和时间。

编辑

现在我这样做是为了更好地控制数据类型和值。

MySqlDataReader LECTOR = cmd.ExecuteReader();

                /**********/ //RS.Load(LECTOR); //This is What I was using

                bool flag = true;

                while (LECTOR.Read()) {
                    if (flag) { //if first time then I create all the columns
                        for (int i = 0; i < LECTOR.FieldCount; i++)
                        {
                            //name of the column
                            RS.Columns.Add(LECTOR.GetName(i));
                            //change the datatype to String So I can manipulate the value later
                            RS.Columns[LECTOR.GetName(i)].DataType = typeof(string);
                        }

                        flag = !flag;

                        DataRow FILA = RS.NewRow();

                        for (int i = 0; i < LECTOR.FieldCount; i++)
                        {/***********************/
                            //I'm Failing here, I don't know how to cring that concat value, it keeps giving me datatype error like System.Byte[] or Byte[] Array
                         /***********************/

                            FILA[LECTOR.GetName(i)] = LECTOR.GetValue(i).ToString();

                        }
                        RS.Rows.Add(FILA);
                    }
                }

那么最好的方法是什么?我正在阅读 MSDN 论坛并尝试一些没有任何结果的方法。

最佳答案

所以为了完全控制我做了这个

MySqlDataReader LECTOR = cmd.ExecuteReader();

                bool flag = true;
                while (LECTOR.Read()) {
                    if (flag) { //first time it creates the columns
                        for (int i = 0; i < LECTOR.FieldCount; i++)
                        {
                            //creates the columns
                            RS.Columns.Add(LECTOR.GetName(i));
                            //Change the column (in this case I can use a conditional statement to be more specific and accurate about the data type)
                            RS.Columns[LECTOR.GetName(i)].DataType = typeof(string);
                        }
                        flag = !flag; // to mark that tthe column creation is done
                    }
                    //create a data row out of the scope
                    DataRow FILA = RS.NewRow();
                    for (int i = 0; i < LECTOR.FieldCount; i++)
                    {
                        //Put the data into the row, using the key (GetName)
                        FILA[LECTOR.GetName(i)] = LECTOR.GetValue(i).ToString();
                        //checks if it is the last case (I dont know why it didn't work if I wrote it after the for)
                        if (i + 1 == LECTOR.FieldCount) { RS.Rows.Add(FILA); }

                    }   
                }

就是这样,这是做更复杂事情的基础。在此之后,我必须考虑最好是在类里面执行此操作,还是每次执行查询时都执行此操作,因为我将不得不查看我需要多久修改一次数据类型。

抱歉我的英语不好。感谢你们指导我做到这一点。

关于c# - 填充 DataGrid : how to manipulate data before showing it on C# MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656443/

相关文章:

c# - 在 Windows 8 中使用 C# 设置菜单 Flyout 背景颜色

php - mysql数据库使用已经在html/php中输入的数据制作下拉菜单

c# - 具有自定义样式 WPF 的 TreeView

c# - 双击listview WPF

WPF Grid.Resources 样式打破了 ResourceDictionary 样式。如何让它们共存?

c# - 从安装项目运行应用程序不返回 UI 区域性

c# - ASP.NET 迁移到 Core (.NET 6.0)

C# 如何记录 DLL 内的文件?

MySql 添加关系而不创建重复项

mysql - 两个用户可以在mysql的同一行上工作吗?