Gridview Update - c#

When I GridView data Update in other forms then data update but when close this form then gridview data refresh not response automatically in C# win-forms application
Here my Code:
private void button1_Click(object sender, EventArgs e)
{
aCatagory=new tbl_Catagory();
aContext = new RM_Inventory_DataContext();
var product = (from p in aContext.tbl_Catagories
where p.CatagoriesID == Convert.ToDecimal(textBox1.Text)
select p).Single();
product.CatagoriesName = textBox2.Text;
aContext.SubmitChanges();
this.Close();
AddCatagories addCatagories=new AddCatagories();
addCatagories.GridView();
}
public void GridView()
{
gridControl1.DataSource = new RM_PM_Inverntory_Management_System.RM_Inventory_DataContext().tbl_Catagories;
}

You are not binding gridview after assigning the datasource.
public void GridView()
{
gridControl1.DataSource = What ever Data Source You have
gridControl1.DataBind();
}

Related

C# Linq To SQL to Fill a DataGrid and and get fields into a TextBox

In C# i can easily get data from a SQL DataBase and put it in a DataGrid with this code :
private void GetPcListBtn_Click(object sender, RoutedEventArgs e)
{
string service = (string)ServiceCB.SelectedValue;
comm = new SqlCommand("select T_COLLABORATEURS.CO_IDENT,T_PC.PC_ID,T_PC.PC_NOM,T_PC.PC_MODEL," +
"T_PC.PC_DATE_MES,T_PC.PC_COMM,T_PC.SERV_ID from T_COLLABORATEURS, T_PC" +
$" where T_COLLABORATEURS.CO_ID = T_PC.CO_ID and T_PC.SERV_ID = '{service}' ", conn);
SqlDataAdapter dap = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
dap.Fill(dt);
PC_DT.ItemsSource = dt.DefaultView;
}
Then on Datagrid SelectionChanged Event i can click on a row to get fields datas and put it in a texbox with this code :
// DataGrid SelectionChanged => Fill TexBox
private void PC_DT_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (PC_DT.SelectedItem is DataRowView oData)
{
string pcNom = (string)oData["PC_NOM"];
string pcModel = (string)oData["PC_MODEL"];
RefPcTxtBox.Text = $"{pcNom} / {pcModel}";
}
}
Now in a other application i want to use Linq To SQL to do exactly the same.
Fill DataGrid :
// Fill DataGrid
private void GetPcListBtn_Click(object sender, RoutedEventArgs e)
{
string service = (string)ServiceCB.SelectedValue;
DataClasses1DataContext dc = new DataClasses1DataContext();
var pcCo = from co in dc.T_COLLABORATEURS
join pc in dc.T_PC on co.CO_ID equals pc.CO_ID
where pc.SERV_ID == service
select new
{
pc.PC_ID,
co.CO_IDENT,
pc.PC_NOM,
pc.PC_MODEL,
pc.PC_DATE_MES,
pc.PC_COMM,
pc.SERV_ID
};
PC_DT.ItemsSource = pcCo;
}
But now how can i fill my TextBox by clicking a DataGrid row ??
private void PC_DT_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// How to Fill my Textbox ??
}
Please try this.
First way:
// Or whenever grid you want to use please set this type
var Grid = sender as DataGrid;
if(Grid != null)
{
textbox = Grid.FieldName
}
Second way:
var currentRow = GridName.SelectedRows;
if(currentRow != null)
{
textbox = currentRow .FieldName
}

Item in ListBox can have different value in textbox?

I have this ListBox that displays items I dragged from my DataGridView. Items in ListBox displays their MenuCode. What I want to happen is I want to show the MenuPrice of each item I have on ListBox on a TextBox. I tried to do this but the MenuCode displays on that TextBox. Please see the image below.
Is it possible to have the MenuPrice displayed on that TextBox? I have done this code but I dont think this is right.
private void menuDataGrid_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) //this code triggers when I dragged an item from datagridview to listbox.
{
menuDataGrid.DoDragDrop(menuDataGrid.CurrentRow.Cells[3].Value.ToString(), DragDropEffects.Copy);
}
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{
pricetxtbox.Text = menuListBox.SelectedItem.ToString();
}
private void ShowDataToGrid() //datagrid code
{
db_connection();
MySqlDataAdapter datagrid = new MySqlDataAdapter();
string selectAll = "SELECT * FROM Menu";
datagrid.SelectCommand = new MySqlCommand(selectAll, connect);
DataTable tbl = new DataTable();
datagrid.Fill(tbl);
BindingSource bs = new BindingSource();
bs.DataSource = tbl;
menuDataGrid.DataSource = bs;
menuDataGrid.Columns[6].Visible = false;
menuDataGrid.Columns[7].Visible = false;
}
I'm going to assume that the MenuPrice is also present on the GridView at a different cell, say Cells[4].
Then you can do something like this
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) //this is the code when I select an item on the listview, then appears at the textbox
{
foreach (DataGridViewRow row in menuDataGrid.Rows)
{
if (row.Cells[3].Value.ToString().Equals(menuListBox.SelectedItem.ToString()))
{
pricetxtbox.Text = row.Cells[4].Value.ToString();
break;
}
}
}
You should pack your data into an object class with properties you want to display and hold. For example:
public class MenuItem()
{
public float MenuPrice { get; set; }
public string MenuCode { get; set; }
}
If you store data of type MenuItem in your ListView, you will be able to do:
private void menuListBox_SelectedValueChanged(object sender, EventArgs e) textbox
{
var item = menu.ListBoxItem.SelectedItem as MenuItem;
if(item == null) return;
pricetxtbox.Text = item.MenuPrice;
}
This would be one way to do it, if you want to enhance your code you should probably consider using databinding for you textboxes.

Binding dynamic datalist in C#

I am trying to create a dynamic Data List bind with database. I can easily create this but I am not able to make the item Command of this Data List. Please help me. Here is my code below
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
categorybinding();
}
}
public void categorybinding()
{
int totalcate = (from x in ebooks.books_category select x).Count();
var ra = (from x in ebooks.books_category select x);
DataList dl = new DataList();
dl.ItemTemplate = new DatalistLabelColumnBind();
dl.DataSource = ra;
dl.DataBind();
form1.Controls.Add(dl);
dl.ItemCommand += new DataListCommandEventHandler(this.ItemCommandHandler);
}
public void ItemCommandHandler(object sender, DataListCommandEventArgs e)
{
Response.Redirect("NewPage.aspx?"+e.CommandArgument.ToString());
}
//Create a new class implementing ITemplate
public class DatalistLabelColumnBind : ITemplate
{
public DatalistLabelColumnBind()
{
//Add constructor
}
public void InstantiateIn(Control container)
{
LinkButton label1 = new LinkButton();
label1.DataBinding += new EventHandler(this.BindLabelColumn);
container.Controls.Add(label1);
}
public void BindLabelColumn(object sender, EventArgs e)
{
LinkButton lbl = (LinkButton)sender;
DataListItem container = (DataListItem)lbl.NamingContainer ;
String strVals = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "books_category1"));
lbl.CommandArgument = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "id_books"));
lbl.Text = strVals;
}
}
My Problem :
My Data List easily added on the page but when I click on the Link Button which is added in the Data List is does not Redirect to the NewPage.aspx
Help me out..
I think your Response.Redirect is not resolving to the intended page.
Try:
Response.Redirect("~/NewPage.aspx?"+e.CommandArgument.ToString());
Write categorybinding() function after the if condition in Page_Load. And ItemCommandHandler will definitely work.

Blank row is added to my DataGridView when working with a binding list

I have a DataGridView that I can add test data to my data grid view but when I want to enter anything manually (through textboxes and drop down boxes), I just get a blank row added to my data grid view
This is the code I have: This is to get to the form where data is entered manually
private void button1_Click(object sender, EventArgs e)
{
frmAddReptile frmAddReptile = new frmAddReptile();
frmAddReptile.ShowDialog();
_ReptileList.Add(_Reptile);
}
Then, on that form I have
internal Reptile _Reptile;
private void button1_Click(object sender, EventArgs e)
{
_Reptile = new Reptile();
_Reptile.Name = txtName.Text;
_Reptile.Country = Convert.ToString(cbxCountry.SelectedItem);
_Reptile.Province = Convert.ToString(cbxProvince.SelectedItem);
_Reptile.Town = Convert.ToString(cbxTown.SelectedItem);
_Reptile.Collector = txtCole.Text;
_Reptile.Length = Convert.ToInt32(txtLen.Text);
_Reptile.Coordinates =txtCo.Text;
_Reptile.Weight = Convert.ToInt32(txtwe.Text);
_Reptile.Date = Convert.ToString(dtpDate.Value.ToShortDateString());
this.Close();
}
Then, once the button is clicked only a black row added to my data grid view.
Ok i have now figured out if i add everything to the samewindows form it works(adds to the data grid view)
private void button1_Click(object sender, EventArgs e)
{
_Reptile = new Reptile();
_Reptile.Name = txtName.Text;
_Reptile.Country = Convert.ToString(cbxCountry.SelectedItem);
_Reptile.Province = Convert.ToString(cbxProvince.SelectedItem);
_Reptile.Town = Convert.ToString(cbxTown.SelectedItem);
_Reptile.Collector = txtCole.Text;
_Reptile.Length = Convert.ToInt32(txtLen.Text);
_Reptile.Coordinates =txtCo.Text;
_Reptile.Weight = Convert.ToInt32(txtwe.Text);
_Reptile.Date = Convert.ToString(dtpDate.Value.ToShortDateString());
_ReptileList.Add(_Reptile);
}
this is what i have now but how can i make it add data form a differnt form
does it have something to do with internal Reptile _Reptile;?

DevExpress DataBinding, Add new Record

I am using DevExpress in my winform application, I have a gridview, data entry form, datanavigator, all bound to dataset.
I want to add new record, if using datanavigator "Add" it works good, how to do the same using a "New Record" button?
BindingSource.AddNew()
is not working, it usually does, but with devexpress its not working.
If you want to use binding then use your objects with binding source..
and use the binding list .AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
event to add new entity object ..
See the example of BindingList on MSDN.
void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
}
DevExpress WinForm Controls works so fast with binding sources as compare to typed datasources etc... YOu can implement bindingSources using these example..
set gridview and the associcated controls datasource to bindsouce that you have created...
process your form with the this MSDN example..
have a look on this code snippet.. may be you will get some idea from this..
private void BindingLIstDemo_Load(object sender, EventArgs e)
{
InitializeListOfEmployees();
BindlstEmp();
listofEmp.AddingNew += new AddingNewEventHandler(listOfEmp_AddingNew);
listofEmp.ListChanged += new ListChangedEventHandler(listofEmp_ListChanged);
}
private void BindlstEmp()
{
lstEmpList.Items.Clear();
lstEmpList.DataSource = listofEmp;
lstEmpList.DisplayMember = "Name";
}
void listofEmp_ListChanged(object sender, ListChangedEventArgs e)
{
MessageBox.Show(e.ListChangedType.ToString());
//throw new NotImplementedException();
}
//declare list of employees
BindingList<Emp> listofEmp;
private void InitializeListOfEmployees()
{
//throw new NotImplementedException();
// Create the new BindingList of Employees.
listofEmp = new BindingList<Emp>();
// Allow new Employee to be added, but not removed once committed.
listofEmp.AllowNew = true;
listofEmp.AllowRemove = true;
// Raise ListChanged events when new Employees are added.
listofEmp.RaiseListChangedEvents = true;
// Do not allow Employee to be edited.
listofEmp.AllowEdit = false;
listofEmp.Add(new Emp(1, "Niranjan", 10000));
listofEmp .Add (new Emp (2,"Jai", 8000));
}
// Create a new Employee from the text in the two text boxes.
void listOfEmp_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Emp (Convert.ToInt32(txtId.Text), txtName.Text,Convert.ToInt32(txtSalary.Text));
}
private void btnAdd_Click(object sender, EventArgs e)
{
Emp empItem = listofEmp.AddNew();
txtId.Text = txtName.Text = txtSalary.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 obj = new Form1();
obj.Show();
}
private void btnDelete_Click(object sender, EventArgs e)
{
var sg = (from sc in listofEmp.ToList<Emp>() where sc.Name == ((Emp)lstEmpList.SelectedValue).Name select sc);
}
private void lstEmpList_SelectedIndexChanged(object sender, EventArgs e)
{
Emp se = listofEmp[lstEmpList.SelectedIndex];
txtId.Text = se.Id.ToString();
txtName.Text = se.Name;
txtSalary.Text = se.Salary.ToString();
}
Here I am using BindingList as datasouce BindingList<Emp> listofEmp; and on the place of grid listing of records are shown in a listbox control.. but all same...try this with your gridview..

Categories

Resources