First of all:
_ddlOptions is drop down list
_selectedOptions is repeater control
and it's just provisional code of my final control.
What I want to do is to get data for _ddlOption on !IsPostBack. There is Add button that enables user to move selected drop down item to repeater control.
It the following way of updating Repeater.Items correct? I found many solution of adding/removing elements manually using DataSource, but here my DataSource is null, as I set it only on !IsPostBack.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_ddlOptions.DataSource = new[] { 1, 2, 3 };
_ddlOptions.DataBind();
}
}
protected void OnAdd(object sender, EventArgs e)
{
var list = new ArrayList(_selectedOptions.Items);
list.Add(_ddlOptions.SelectedItem);
_ddlOptions.Items.RemoveAt(_ddlOptions.SelectedIndex);
_selectedOptions.DataSource = list;
_selectedOptions.DataBind();
}
If you only need to fetch data once and you're going to use viewstate, get the data first time you need it, store it in VS and get it from VS for all future PostBacks.
Example:
public List<int> Data
{
get
{
if (ViewState["Data"] == null)
{
// Get your data, save it and return it.
var data = new List<int> { 1, 2, 3 };
ViewState["Data"] = data;
return data;
}
return (List<int>)ViewState["Data"];
}
set
{
ViewState["Data"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData(Data);
}
}
private void BindData(List<int> data)
{
_ddlOptions.DataSource = data;
_ddlOptions.DataBind();
}
protected void OnAdd(object sender, EventArgs e)
{
var existing = Data;
existing.Add(_ddlOptions.SelectedItem);
_ddlOptions.Items.RemoveAt(_ddlOptions.SelectedIndex);
Data = existing;
BindData(existing);
}
I didn't test this - and its only my first thought but you can build on it from here.
Patrick.
Looks good to me. You just may want to move the decalration for your list outside the onAdd method. As you have it I think it will be reinitialized every time the add button is clicked, so you'll never have more than the currently selected item in your repeater.
You can use a DataAdapter to fill a table in a DataSet.
DataSet ds = new DataSet();
using (SqlConnection conn = YourConnectionFactory.GetConnection())
{
SqlCommand objComm = DBHelper.CreateStoredProc("YourStoredProcedure",
conn);
SqlDataAdapter adapt = new SqlDataAdapter(objComm);
adapt.Fill(ds, TableName);
conn.Close();
}
DataTable dt = ds.Tables[0];
for (int a=dt.Rows.Count-1; a>= 0; a--)
{
// check and insert as necessary
}
YourControl.DataSource = ds;
YourControl.DataBind();
You can also do something like this then,
like rebind Taken from: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx:
Dim values As New ArrayList()
values.Add(New PositionData("Microsoft", "Msft"))
values.Add(New PositionData("Intel", "Intc"))
values.Add(New PositionData("Dell", "Dell"))
Repeater1.DataSource = values Repeater1.DataBind()
Repeater2.DataSource = values Repeater2.DataBind()
Related
I added rows in datagridview manually from the UI.
Now i want the datasource to fill datatable but datasouce is empty.
Since you have manually added rows to your GridView and you have not set DataSource so obviously datasource will be empty or null,
you just need to iterate all the rows and bind them to datatable.
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Particular", typeof(string));
dt.Columns.Add("Debit", typeof(int));
dt.Columns.Add("Credit", typeof(int));
dt.Columns.Add("Balance", typeof(string));
dt.Columns.Add("Summary", typeof(string));
private void bindGridtoDataTable()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
dt.Rows.Add(row.Cells["Date"].Value, row.Cells["Particular"].Value, row.Cells["Debit"].Value, row.Cells["Credit"].Value, row.Cells["Balance"].Value, row.Cells["Summary"].Value);
}
Here is a nice example of how to do this: Save datagridView into sql database
You need to differentiate if rows have been deleted, modified or added.
var dataTable = ((DataTable)dataGridView1.DataSource).GetChanges();
if(dataTable != null && dataTable.Rows.Count > 0)
{
foreach (DataRow row in dataTable.Rows)
{
switch (row.RowState)
{
case DataRowState.Added:
// DO INSERT QUERY
break;
case DataRowState.Deleted:
// DO DELETE QUERY
break;
case DataRowState.Modified:
SqlCommand command = new SqlCommand("UPDATE YOURTABLE SET TypNastaveniaID = #typ, Nazov = #title, Hodnota = #amount");
command.Parameters.Add(new SqlParameter("#typ", row["TypNastaveniaID"]));
command.Parameters.Add(new SqlParameter("#title", row["Nazov"]));
command.Parameters.Add(new SqlParameter("#amount", row["Hodnota"]));
command.ExecuteNonQuery();
break;
}
}
((DataTable)dataGridView1.DataSource).AcceptChanges();
}
Some answers here suggest you to fiddle with Rows from the DataGridView. I strongly advise you not to do this, but use a BindingSource and a BindingList!
I think the problem is because you assign your collection directly to a DataSource, instead of via a BindingList
If you have a class, say MyClass of which you have a sequence that you want to show in your DataGridView, and you want to Add / Remove / Change items from this sequence by adding / removing / changing the rows in your DataGridView you should do the following
Get your items as `IEnumerable
Wrap them in a BindingList
Assign this to bindingSource1.DataSource
Assign the bindingSource to dataGridView1.DataSource
Most of this can be done in the visual studio designer. The only thing you have to do is wrap your data in the BindingList and assign the BindingList to the DataSource of the BindingSource:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponents();
}
private void InitialeDataGridView
{
IEnumerable<MyClass> dataToShow = ...
this.BindingSource1.DataSource = new BindingList<MyClass>(dataToShow.ToList());
}
private void Form1_Load(object sender, ...)
{
this.InitializeDataGridView();
}
private void button1_click(object sender, ...)
{
IEnumerable<MyClass> editedData = (BindingList<MyClass>)this.bindingSource1.DataSource;
ProcessEditedData(editedData);
}
}
You'll see all your edited data. However, your data won't be sortable. To do this, you can create a SortableBindingList, somewhere described here on Stackoverflow. However it's much easier to install Nuget BindingListView
This will slightly change your code, but enables sorting and filtering:
Instead of a BindingList you use a BindingListView<MyClass>. Your code will change only slightly:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponents();
this.myItems= new BindingListView<MyClass>(this.components);
this.bindingSource1.DataSource = this.myItems;
}
private readonly BindingListView<MyClass> myItems;
private void InitialeDataGridView
{
IEnumerable<MyClass> dataToShow = ...
this.myItems = dataToShow.ToList();
}
private void Form1_Load(object sender, ...)
{
this.InitializeDataGridView();
}
private void button1_click(object sender, ...)
{
IEnumerable<MyClass> editedData = (BindingListView<MyClass>)this.bindingSource1.DataSource;
ProcessEditedData(editedData);
}
}
And presto, you're columns are sortable by clicking on the column header. See the automatic changing of the sorting-glyph in the sorted column header, you'll get that as an extra bonus.
Filtering:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (this.checkBox1.Checked)
{ // show only items with Id == 0
Predicate<MyClass> pr = p => p.Id == 0;
this.persons.ApplyFilter(pr);
}
else
this.persons.RemoveFilter();
}
It's that easy: add / remove / change / sort / filter: all with a few lines of code.
I'm completely new to databases and EF but I made a database with EF and have a DataGridView control on a windows form that I made by dragging my datasource to my form. After the user enters their information and hits the save button it succesfully saves their information in the database using this code
public partial class bsMainPage : Form
{
BSDATAContainer db = new BSDATAContainer();
public bsMainPage()
{
InitializeComponent();
}
private void saveBtn_Click(object sender, EventArgs e)
{
BSRecords breakfastRecord = new BSRecords();
breakfastRecord.BS = brkBS.ToString();
breakfastRecord.Carbs = brkCarb.ToString();
breakfastRecord.Notes = brkftNoteTxt.Text;
breakfastRecord.Date = dateTxt.Text;
BSRecords lunchRecord = new BSRecords();
lunchRecord.BS = lchBS.ToString();
lunchRecord.Carbs = lchCarb.ToString();
lunchRecord.Notes = lnchNoteTxt.Text;
lunchRecord.Date = dateTxt.Text;
BSRecords dinnerRecord = new BSRecords();
dinnerRecord.BS = dnrBS.ToString();
dinnerRecord.Carbs = dnrCarb.ToString();
dinnerRecord.Notes = dnnrNoteTxt.Text;
dinnerRecord.Date = dateTxt.Text;
db.BSRecords.Add(breakfastRecord);
db.BSRecords.Add(lunchRecord);
db.BSRecords.Add(dinnerRecord);
db.SaveChanges();
}
}
But it doesn't show up in the database until I restart the program. When the user selects a row in the DataGridView and hits the delete button which has this code
private void deleteRowsBtn_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.bSRecordsDataGridView.SelectedRows)
{
bSRecordsDataGridView.Rows.RemoveAt(item.Index);
}
db.SaveChanges();
}
It deletes the data in the DataGridView but doesn't save the changes in my database. I have followed all the answers I found on here and other sites to delete in the database but nothing will save the deleted changes. Does anyone have any idea how to make it work?
You can delete it using remove. You will need to get the key/id field so without seeing the grid and assuming it is say in a hidden first column:
private void deleteRowsBtn_Click(object sender, EventArgs e)
{
string delId;
BSRecords deleteRecord;
foreach (DataGridViewRow item in this.bSRecordsDataGridView.SelectedRows)
{
bSRecordsDataGridView.Rows.RemoveAt(item.Index);
// code to remove record from database
delId = item.Cells[0].Value.ToString(); // column that has id field
deleteRecord = db.BSRecords.First(b => b.Id == delId); // get the record. will throw exception if not found.
db.BSRecords.Remove(deleteRecord);
}
db.SaveChanges();
bSRecordsDataGridView.DataBind(); // this will refresh your grid. Do same in save.
}
Also note you can rewrite this code:
BSRecords breakfastRecord = new BSRecords();
breakfastRecord.BS = brkBS.ToString();
breakfastRecord.Carbs = brkCarb.ToString();
breakfastRecord.Notes = brkftNoteTxt.Text;
breakfastRecord.Date = dateTxt.Text;
with an object initializer:
BSRecords breakfastRecord = new BSRecords { BS = brkBS.ToString(),
Carbs = brkCarb.ToString(),
Notes = brkftNoteTxt.Text,
Date = dateTxt.Text };
I have a form that allows a user to add players to a roster, by entering the player name and selecting, from a combo box, the division to which the player belongs.
When time comes to add the player to my TreeView control, the node that should display the division selected displays this text instead: System.Data.DataRowView
I got the code to implement this through MSDN here: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem%28v=vs.110%29.aspx
Here's the code in the load function of the form, to fill the combo box:
private void frm_add_players_Load(object sender, EventArgs e)
{
Divisions divs = new Divisions();
Players players = new Players();
DataTable dtDivisions = divs.GetActiveDivisions(); //divisions combo box
DataTable dtPlayers = players.GetPlayersByTourID(this.tourID);
//set the forms datatable
this.dt_players = dtPlayers;
//fill the combo box
this.cmbo_divisions.DataSource = dtDivisions;
this.cmbo_divisions.DisplayMember = "title";
this.cmbo_divisions.ValueMember = "ID";
this.cmbo_divisions.SelectedIndex = -1;
this.cmbo_divisions.Text = "Select a Division";
//set treeview imagelist
this.tview_roster.ImageList = tview_imagelist;
this.tview_roster.ImageIndex = 1; //division icon
//fill treeview
foreach (DataRow dr in dtPlayers.Rows)
{
FillPlayerTreeview(dr);
}
//expand treeview
this.tview_roster.ExpandAll();
this.ActiveControl = this.txt_player_name;
}
Here I call the function to add the player to the TreeView:
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, selItem.ToString());
}
And here is the function that adds the player:
private void AddPlayerToTreeView(string playerName, string division)
{
TreeNode[] tns = this.tview_roster.Nodes.Find(division, false); //try to find the division, if exists
TreeNode tn = new TreeNode();
if (tns.Length > 0) //division exists - add player
{
tn = this.tview_roster.Nodes[tns[0].Index].Nodes.Add(playerName, playerName);
tn.ImageIndex = 0; //player icon
}
else //division doesn't exist - add division, then add player
{
tn = this.tview_roster.Nodes.Add(division, division);
tn.ImageIndex = 1; //division icon
AddPlayerToTreeView(playerName, division);
}
}
And the result is this:
I'm not sure why it won't work.. and I'm at a loss. Any help would be appreciated.
Well, well... maybe something like the following.
Access the combo's data source, which is a DataTable, and extract selected row and column value using selected index. Maybe add some error handling, too.
private void btn_add_Click(object sender, EventArgs e)
{
var data = cmbo_divisions.DataSource as DataTable;
var row = data.Rows[cmbo_divisions.SelectedIndex];
var selected = row["title"].ToString();
AddPlayerToTreeView(txt_player_name.Text, selected);
}
Try this :
private void btn_add_Click(object sender, EventArgs e)
{
object selItem = cmbo_divisions.SelectedItem;
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.SelectedItem as string);
}
ToString() will get the type name, but in that case the SelectedItem is a string.
Try with:
private void btn_add_Click(object sender, EventArgs e)
{
AddPlayerToTreeView(txt_player_name.Text, cmbo_divisions.Items[cmbo_divisions.SelectedIndex].Text);
}
EDIT: Updated to a better way
On Button click event I execute a new query i get a new table but i am unable to populate it on to the same grid view. Is there any why where i can use schema instead of dataTables.
private void GetData()//This method Displays the Datatable onto the Grid
{
int intCount = 0;
int.TryParse(txt_messagecount.Text, out intCount);
DataTable dtData = null;
//dtData = _objIMRSData.GetData(txt_fromdate.Text, txt_todata.Text, txt_messagecount.Text);
dtData = _objIMRSData.GetTransactionData( txt_fromdate.Text, txt_todata.Text,intCount );
grd_transactionLog.DataSource = dtData;
grd_transactionLog.DataBind();
dtData.Clear();
}
//On Button click event I execute a new query i get a new table
//but i am unable to populate it on to the same grid view
protected void btn_next_Click(object sender, EventArgs e)
{
int messageCount = int.Parse(txt_messagecount.Text);
string lastRecord = grd_transactionLog.Rows[messageCount-1].Cells[1].Text;
DataTable dtData1 = null;
//dtData = _objIMRSData.GetData(lastRecord, txt_todata.Text, txt_messagecount.Text);
dtData1 = _objIMRSData.GetTransactionData(lastRecord, txt_todata.Text, messageCount);
//grd_transactionLog.Columns.Clear();
grd_transactionLog.DataSource = dtData1;
grd_transactionLog.DataBind();
dtData1.Clear();
}
Reassign the datasource. In the click event do this.
grd_transactionLog.DataSource = null;
grd_transactionLog.DataSource = dtData1;
I need to fill the dropdown value from database passing string value.here i am gettin filled dataset from database but it is not binding so that i am unable to fill properly the dropdown and select items from dropdown.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strCreatedId = string.Empty;
strCreatedId = "2";
fillgroupname(id_ddlgroupname,strCreatedId);
}
}
public void fillgroupname(DropDownList id_ddlgroupname, string strCreatedId)
{
DataSet dsgroup = new DataSet();
dsgroup = objUser.GetFillGroup(strCreatedId);
if (dsgroup.Tables.Count > 0)
{
if (dsgroup.Tables[0].Rows.Count > 0)
{
this.id_ddlgroupname.DataSource = dsgroup;
this.id_ddlgroupname.DataTextField = "c_group_name";
this.id_ddlgroupname.DataValueField = "c_group_name";
this.id_ddlgroupname.DataBind();
this.id_ddlgroupname.Items.Insert(0, "--Select--");
}
}
I will upload image as it appears.
Image1:
Image2:
Image3:
like this the dropdown issue i am facing i am not able to know where i am going wrong.pls help me.
Use this code
public void fillgroupname(DropDownList id_ddlgroupname, string strCreatedId)
{
DataSet dsgroup = new DataSet();
dsgroup = objUser.GetFillGroup(strCreatedId);
id_ddlgroupname.DataSource = dsgroup;
id_ddlgroupname.DataTextField = "c_group_name";
id_ddlgroupname.DataValueField = "c_group_name";
id_ddlgroupname.DataBind();
ListItem li = new ListItem("--Select--","0");
id_ddlgroupname.Items.Insert(0, li);
}
This is working for me, I hope it will be use full for you.
sorry. I got solved my issue it was problem in my validation using jquery.