【问题标题】:Trouble using datagrid (or datagridview) in wpf or winform在 wpf 或 winform 中使用 datagrid(或 datagridview)时遇到问题
【发布时间】:2013-09-02 23:14:30
【问题描述】:

我有一个带有小型数据库的数据网格(sql compact) 表格包含500多行,每个块包含不同的节号;

Xamle 代码:

  <Window x:Class="WpfApplication9.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="440" Width="690" Loaded="Window_Loaded">
<Grid>
    <Button Content="To TextBox" Height="29" HorizontalAlignment="Left" Margin="561,272,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <DataGrid AutoGenerateColumns="True" Height="234" HorizontalAlignment="Left" Margin="12,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="644" />
    <CheckBox Content="Section Rate" Height="16" HorizontalAlignment="Left" Margin="126,313,0,0" Name="checkBox1" VerticalAlignment="Top" />
    <CheckBox Content="Section Result" Height="16" HorizontalAlignment="Left" Margin="28,313,0,0" Name="checkBox2" VerticalAlignment="Top" />
    <CheckBox Content="Discipline" Height="16" HorizontalAlignment="Left" Margin="28,272,0,0" Name="checkBox3" VerticalAlignment="Top" />
    <TextBox Height="54" HorizontalAlignment="Left" Margin="240,324,0,0" Name="textBox1" VerticalAlignment="Top" Width="396" />
    <CheckBox Content="Total Biology" Height="16" HorizontalAlignment="Left" Margin="113,272,0,0" Name="checkBox4" VerticalAlignment="Top" />
    <CheckBox Content="Math" Height="16" HorizontalAlignment="Left" Margin="28,351,0,0" Name="checkBox5" VerticalAlignment="Top" />
    <CheckBox Content="Physics" Height="16" HorizontalAlignment="Left" Margin="113,351,0,0" Name="checkBox6" VerticalAlignment="Top" />
</Grid>

背后的代码:

private void button1_Click(object sender, RoutedEventArgs e)
    {
        DataRowView _data = dataGrid1.CurrentCell.Item as DataRowView;
        if (_data != null)
        {
            MessageBox.Show(_data.Row[0].ToString());
        }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\MyDatabase1.sdf");
        con.Open();
        SqlCeDataAdapter da = new SqlCeDataAdapter("select * from StudentGroup", con);
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        da.Fill(dt);
        dataGrid1.ItemsSource = dt.DefaultView;
        //dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
        con.Close();
    }

我想根据选中的复选框将数据导出到 textbox1,格式如下: 第 1 行:组号、组名、学科(或总生物学:生物学列中的行结果总和) 对于选定组编号中的每个部分编号: 下一行:(部分速率或部分结果,数学或物理)==> 基于选中的复选框

例如:

第 1 行:1,GPA,100(或 137= 总生物学) 如果 SectionResultcheckbox 和 mathchechbox 都被选中: 下一行将是 :80 , 80, 90 ,70 ,54 ,31

我已经试过了:

    DataRowView _data = dataGrid1.CurrentCell.Item as DataRowView;
        if (_data != null)
        {
            MessageBox.Show(_data.Row[0].ToString());
        }

但它不起作用。

我不知道如何处理这个问题? 感谢您的帮助。

【问题讨论】:

  • 问题是什么?什么不工作?你有什么问题?不知道病就不能开药:)
  • @ 没有人:我已经解释过:我想根据选中的复选框将数据导出到 textbox1,格式如下:第 1 行:组号、组名、学科(或总生物学:结果总和生物学列中的行数)以及所选组号中的每个部分编号:下一行:(部分速率或部分结果,数学或物理)==>基于选定的复选框
  • @SmartMan 选中Discipline 复选框时会发生什么?...输出应该是什么??
  • @Kyle :如果选中了 Discipline 复选框,那么 textbox1 中的第 1 行将是: Group Number , GroupName , Discipline

标签: c# wpf winforms datagrid


【解决方案1】:

如果 Grid SelectionUnit 设置为“FullRow”,则使用以下代码。

((DataRowView)dgGroup.SelectedItem).Row -> This will give you current data row

((DataRowView)dgGroup.SelectedItem).Row.ItemArray[0] -> This will give first column value

((DataRowView)dgGroup.SelectedItem).Row.ItemArray[1] -> This will give second column value

如果 Grid SelectionUnit 设置为“单元格”,则使用以下代码。

(DataRowView)dgGroup.SelectedCells[0].Item -> This will give you current data row

((DataRowView)dgGroup.SelectedCells[0].Item).Row.ItemArray[0] -> This will give first column value

根据您的评论添加更多代码以循环浏览各个部分。

            // Find index of selected row which would be group/block row. Add 1 to find first section row in that block/group.
        var index = dgGroup.Items.IndexOf(dgGroup.SelectedItem) + 1;

        // Starting from the index found above loop through section rows untill you find blank row which can be identified by checking if "Group Name" does not have any value.
        for (int i = index; i < dgGroup.Items.Count; i++)
        {
            if (((DataRowView)dgGroup.Items[i]).Row.ItemArray[1].ToString().Trim() == string.Empty)
            {
                return;
            }
            else
            {
                // Add data to textbox.
            }
        }

为了改善使用体验,您还可以考虑设置一些限制,以便用户只能选择组/块而不是部分。如果这适合您的需要。

【讨论】:

  • 谢谢,但我测试了你的答案: textBox1.Text=((DataRowView)dgGroup.SelectedItem).Row.ToString 给出 System.Data.DataRowView 但 ((DataRowView)dgGroup.SelectedItem).Row.ItemArray [0] 有效。我如何循环遍历行直到空行?如您所见,每个块由一个空行分隔。谢谢。
  • @RonakThakkar:嗨,你介意帮助我吗?多谢。 stackoverflow.com/questions/19110613/…
【解决方案2】:

感谢 RonakThakkar,我对他有用的答案投了赞成票。

由于 Grid SelectionUnit 设置为“FullRow”所以我的问题的第一部分答案是:

private void button1_Click(object sender, RoutedEventArgs e)
{

    if (((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[0].ToString() != string.Empty)
    {
        textBox1.Text=((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[0].ToString()+","+((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[1].ToString()+","+((DataRowView)dataGrid1.SelectedItem).Row.ItemArray[7].ToString();
    }
}

文本框中的结果将是:1,GPA,100

所以剩下的部分是如何循环遍历行直到分隔每个块的空行?

更多解释:例如,如果用户选择第一行,并且 sectionresultcheckbox 和 mathcheckbox 被选中,并且单击按钮,我们应该在文本框中: 1,GPA,100 +下一行 +80 , 80, 90 ,70 ,54 ,31 感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2020-03-12
    • 2018-01-02
    • 2021-08-23
    相关资源
    最近更新 更多