如何使用c#将文本框内容添加到wpf中的datagrid [英] how to add textbox contents to datagrid in wpf using c#

查看:55
本文介绍了如何使用c#将文本框内容添加到wpf中的datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框和一个添加按钮和一个带有2个coloumns的数据网格。我需要做的是当我按下按钮时,文本框中的内容应该移动到数据网格中。文本框名称是textbox1和textbox2.第一个文本框中的内容应该移到datagrid中的第一个coloumn,textbox2的内容到datagrid.Next内容的第二个coloumn应该移动到下一行等等。有人请帮帮我。谢谢提前< br $>




这是我的XAML代码:

i have two textboxes and an add button and a datagrid with 2 coloumns.what i need to do is when i press the button the contents in the textboxes should move into a datagrid. textboxes names are textbox1 and textbox2.the contents in first textbox should move to first coloumn in datagrid,and contents of textbox2 to second coloumn of datagrid.Next contents should move to next row and so on.Someone please help me.Thanks in advance


This is my XAML code:

<window x:class="WpfApplication9.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
        Title="MainWindow" Height="347" Width="570" DataContext="{Binding}" Loaded="Window_Loaded">
    <grid>
        <textbox height="23" horizontalalignment="Left" margin="34,32,0,0" name="textbox1" verticalalignment="Top" width="120" textchanged="textbox1_TextChanged" />

        <textbox height="23" horizontalalignment="Left" margin="34,205,0,0" name="textbox2" verticalalignment="Top" width="120" textchanged="textbox2_TextChanged" />

        <Button Content="ADD" Height="23" HorizontalAlignment="Left" Margin="193,108,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" >

        <datagrid autogeneratecolumns="False" removed="Gray" height="311" itemssource="{Binding}" horizontalalignment="Right" name="datagrid1" verticalalignment="Center" width="202" selectionchanged="dataGrid1_SelectionChanged" datacontext="{Binding}" canuseraddrows="{Binding ElementName=dataGrid1}" canuserdeleterows="{Binding ElementName=dataGrid1}" minrowheight="1" rowremoved="#FF95FFFF" margin="0,-2,0,0" fontweight="Bold">

           <datagrid.columns>
                <datagridtextcolumn header="Name" width="100" canuserresize="False" binding="{Binding}" canuserreorder="False" />

                <datagridtextcolumn header="ID" width="100" canuserresize="False" binding="{Binding}" canuserreorder="False" />

                </datagrid.columns>
        </datagrid>
    </grid>
</window>













这是我的Xaml.cs代码









And this is my Xaml.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication9
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SetInitialValues();
        }
     
          void SetInitialValues()
           {
            textbox1.Text = "";
			textbox2.Text = "";
           }

            private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {

            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {

            }

            private void textbox1_TextChanged(object sender, TextChangedEventArgs e)
            {

            }

            private void textbox2_TextChanged(object sender, TextChangedEventArgs e)
            {

            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {

            }


    }
}

推荐答案

并更新您的代码隐藏如下



and Update your codebehind as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;

namespace RowDetailTemplate
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("ID");
            DataRow dr = dt.NewRow();
            dr["Name"] = "Raman";
            dr["ID"] = "Raman";
            dt.Rows.Add(dr);
            datagrid1.ItemsSource = dt.DefaultView;
        }
        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void textbox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textbox2_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DataView dv = datagrid1.ItemsSource as DataView;
            DataTable dt = dv.Table;
            DataRow dr = dt.NewRow();
            dr["Name"] = textbox1.Text;
            dr["ID"] = textbox2.Text;
            dt.Rows.Add(dr);
        }
 
    }
}







并将您的xaml更新为:






and update your xaml as:

<window x:class="RowDetailTemplate.Window3" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:wpf="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="Window3" Height="300" Width="300"&gt;
    <grid>
        <textbox height="23" horizontalalignment="Left" margin="12,12,0,0" x:name="textbox1" verticalalignment="Top" width="120" textchanged="textbox1_TextChanged" />

        <textbox height="23" margin="0,12,12,0" x:name="textbox2" verticalalignment="Top" textchanged="textbox2_TextChanged" horizontalalignment="Right" width="120" />
        <wpf:datagrid autogeneratecolumns="False" x:name="datagrid1" selectionchanged="dataGrid1_SelectionChanged" datacontext="{Binding DGSource}" canuseraddrows="{Binding ElementName=dataGrid1}" canuserdeleterows="{Binding ElementName=dataGrid1}" minrowheight="1" margin="12,41,12,79" fontweight="Bold" xmlns:wpf="#unknown">
            <wpf:datagrid.columns>
                <wpf:datagridtextcolumn header="Name" width="100" canuserresize="False" binding="{Binding Name}" canuserreorder="False" />
                <wpf:datagridtextcolumn header="ID" width="100" canuserresize="False" binding="{Binding ID}" canuserreorder="False" />
            </wpf:datagrid.columns>
        </wpf:datagrid>
        <button name="btn" content="click" margin="33,0,29,20" height="35" click="button1_Click" verticalalignment="Bottom" />
    </grid>
</window>


只需在按钮点击事件上编写以下代码



Just write following code on button click event

#Fetch grid datasource as datatable and update datatable and bind with grid again

  DataView dv = datagrid1.ItemsSource as DataView;
            DataTable dt = dv.Table;
            DataRow dr = dt.NewRow();
            dr["Column1Name"] = textbox1.Text;
            dr["Column2Name"] = textbox2.Text;
            dt.Rows.Add(dr);

//Again Bind Datatable with grid 
 grid1.ItemsSource = dt.DefaultView;





如果您需要解决方案作为示例我可以转发你........



如果您发现这是正确的答案请将其标记为已接受的答案



if you need solution as an example i can forward you ........

If you find this is right answer plese mark it as accepted answer


DataTable dt = new DataTable();
        public MainWindow()
        {
            InitializeComponent();
           
            dt.Columns.Add("column1");
            dt.Columns.Add("column2");
            dataGrid1.ItemsSource = dt.DefaultView;
        }
 

    private void button1_Click(object sender, RoutedEventArgs e)
        {
            DataRow row = dt.NewRow();
            row[0] = textBox1.Text;
            row[1] = textBox2.Text;
            dt.Rows.Add(row);
            dataGrid1.ItemsSource = dt.DefaultView;
        }


这篇关于如何使用c#将文本框内容添加到wpf中的datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆