I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component public class AllViewComponent : ViewComponent { private readonly UserManager<ApplicationUser> _userManager; public AllViewComponent(UserManager<ApplicationUser> userManager) { _userManager = userManager; } public async Task<IViewComponentResult> InvokeAsync() { List<StudentViewModel> allUsers = new List<StudentViewModel>(); var items = await _userManager.Users.ToListAsync(); foreach (var item in items) { allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email }); }
After binding the DataGrid in wpf. You can insert some item in the database table using WPF TextBox. So first of all add one label , TextBox and one button control in the grid container. Insert data in the table on button click so add button click event on button.
<Grid>
<Grid Margin="0,0,10,203">
<Label Content="Enter Name Here:" /> <TextBox Name="text1" Text="" Margin="112,4,228,0" Height="27" VerticalAlignment="Top" />
<Button Content="Add Data" Margin="76,46,310,22" Click="Button_Click" />
</Grid>
<DataGrid Name="g1" AutoGenerateColumns="False" Margin="110,121,137,57">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="Id" Width="100"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
If you want to show inserted item then need to bind DataGrid along with other control. Check my previous article.
C# Code
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
binddatagrid();
}
private void binddatagrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["connstudent"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from [Students]";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Students");
da.Fill(dt);
g1.ItemsSource = dt.DefaultView;
con.Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["connstudent"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into [Students](Name)values(@nm)";
cmd.Parameters.AddWithValue("@nm", text1.Text);
cmd.Connection = con;
int a = cmd.ExecuteNonQuery();
if(a==1)
{
MessageBox.Show("Data add Sucessfully");
binddatagrid();
}
}
}
}
Above mentioned code is similar to all application like asp.net, windows form etc. Apply sql insert query with the SqlCommand class and execute NonQuery() method to insert the item in the database table.
<Grid>
<Grid Margin="0,0,10,203">
<Label Content="Enter Name Here:" /> <TextBox Name="text1" Text="" Margin="112,4,228,0" Height="27" VerticalAlignment="Top" />
<Button Content="Add Data" Margin="76,46,310,22" Click="Button_Click" />
</Grid>
<DataGrid Name="g1" AutoGenerateColumns="False" Margin="110,121,137,57">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="Id" Width="100"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="100"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
If you want to show inserted item then need to bind DataGrid along with other control. Check my previous article.
C# Code
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
binddatagrid();
}
private void binddatagrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["connstudent"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from [Students]";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Students");
da.Fill(dt);
g1.ItemsSource = dt.DefaultView;
con.Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["connstudent"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into [Students](Name)values(@nm)";
cmd.Parameters.AddWithValue("@nm", text1.Text);
cmd.Connection = con;
int a = cmd.ExecuteNonQuery();
if(a==1)
{
MessageBox.Show("Data add Sucessfully");
binddatagrid();
}
}
}
}
Above mentioned code is similar to all application like asp.net, windows form etc. Apply sql insert query with the SqlCommand class and execute NonQuery() method to insert the item in the database table.
Comments
Post a Comment