updating view from gridview - asp.net

My query is
SELECT
deldate,
transno,
matno,
MAT_NAME,
rawpkgno,
MAX(SWITCH(deldate=? ,ORDCASES )) AS [CURRDATE],
MAX(SWITCH(deldate=? ,ORDCASES )) AS [previous_day],
MAX(SWITCH(deldate=? ,ORDCASES )) AS [Last_date]
FROM
invorder
WHERE
invorder.strno =54009
OR [invorder.deldate] IS NULL
GROUP BY
matno,
MAT_NAME,
rawpkgno,
transno,
deldate
I want to edit this from gridview. As this is view I cannot edit it from gridview. But is their any way through which previous_day, Last_date columns can be constant and I can only make changes to CURRDATE column. Kindly help me.

Hi you have to bind the row_added event on grid and there make the all the cell read only except u want editable.. check the below code..
public Form1()
{
InitializeComponent();
dataGridView1.RowsAdded += new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
}
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
foreach (DataGridViewCell item in dataGridView1.Rows[e.RowIndex].Cells)
{
// here i used the 5 because i want to make the 5th index cell as editable.
if (item.ColumnIndex == 5)
{
continue;
}
item.ReadOnly = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bind_your_data_source_here;
dataGridView1.Refresh();
}
you can find the sample from here..
Download

Related

Getting gridview data in variables and inserting it to database

I am making a shopping cart type of thing using grid view user will keep adding item in it after doing it so user will click save i want to get values from all columns of grid view each row at a time and save it to database.
here is the code for grid view load
protected void add_Click(object sender, EventArgs e)
{
product.Item = Item_Drop.SelectedItem.Text;
product.Quantity = quantity_box.Text;
int qun =Convert.ToInt32(quantity_box.Text);
int unitP= Convert.ToInt32(unitPrice_box.Text);
product.item_Toal = (qun*unitP).ToString();
list.Add(product);
temp_gridView.DataSource = list;
temp_gridView.DataBind();
}
And here is what i am trying to get values
public void Values_from_grid()
{
foreach(temp_gridView row in temp_gridView.Rows)
{
for(int i = 0; i < temp_gridView.Columns.Count, i++)
{
String header = temp_gridView.Columns[i].HeaderText;
String cellText = row.Cells[i].Text;
}
}
}
i am not getting any values in "header" or "cellText" . .. .
Use this code to get header row value .
GridViewName.HeaderRow.Cells[0].Text;
Found Answer no need to use
GridView.Rows.Count
just use a simple int to iterate
int i = 0;
foreach(GridViewRow row in temp_gridView.Rows)
{
box.Item_id = temp_gridView.Rows[i].Cells[0].Text;
box.Quantity = temp_gridView.Rows[i].Cells[1].Text;
box.Total = temp_gridView.Rows[i].Cells[2].Text;
i++;
}

ASPxComboBox Filtering Large DataSet Using XpoDataSource (DevExpress)

I tried to Filter an ASPxComboBox that gets data using an XpoDataSource, note that restoring and filtering data from a small data set works fine , the issue start when I try to filter large dataset - about 70000 records- from datasource the ComboBox loading becomes very slow since XpoDataSource gets all data from database table .
So I created a criteria for the XpoDataSource to reduce number of records restored ,then the ComboBox Keeps repeating the top 10 records while scrolling down the ComboBox, I don't know where the problem is.
I realized that what I need is similar to the example in the following link
But using an XpoDataSource instead of SqlDataSource1 .
I don't know how to write a similar code for an XpoDataSource .
this is my code :
protected void cmbServices_OnItemRequestedByValue_SQL(object source, DevExpress.Web.ASPxEditors.ListEditItemRequestedByValueEventArgs e)
{
try
{
string criteria = "";
if (string.IsNullOrEmpty(e.Value.ToString()) || e.Value.ToString().Length < 3)
{
criteria = "1 = 2";
}
else
{
criteria =
string.Format("(( Code like '{0}%' OR ProductName like '{0}%') AND CustomerId = {1})", e.Value.ToString(), (cmbServicesActivities != null && cmbServicesActivities.Value != null) ? cmbServicesActivities.Value.ToString() : "0");
}
dsServices.Session = LookupsSession;
dsServices.Criteria = criteria;
cmbServicesDescription.DataSource = dsServices;
cmbServicesDescription.DataBind();
}
catch (Exception exc)
{
Debug.WriteLine(exc.Message);
}
}
The Following Example demonstrates the answer of my question
public partial class _Default : System.Web.UI.Page {
Session session = XpoHelper.GetNewSession();
protected void cmb_ItemRequestedByValue(object source, DevExpress.Web.ASPxEditors.ListEditItemRequestedByValueEventArgs e) {
MyObject obj = session.GetObjectByKey<MyObject>(e.Value);
if (obj != null) {
cmb.DataSource = new MyObject[] { obj };
cmb.DataBindItems();
}
}
protected void cmb_ItemsRequestedByFilterCondition(object source, DevExpress.Web.ASPxEditors.ListEditItemsRequestedByFilterConditionEventArgs e) {
XPCollection<MyObject> collection = new XPCollection<MyObject>(session);
collection.SkipReturnedObjects = e.BeginIndex;
collection.TopReturnedObjects = e.EndIndex - e.BeginIndex + 1;
collection.Criteria = new BinaryOperator("Title", String.Format("%{0}%", e.Filter), BinaryOperatorType.Like);
collection.Sorting.Add(new SortProperty("Oid", DevExpress.Xpo.DB.SortingDirection.Ascending));
cmb.DataSource = collection;
cmb.DataBindItems();
}

Removing an Object from a DataSource

I am trying to remove an item which is selected in a datagrid from the domaindatasource that it uses, through a button click.
The code for the button click is:
private void medItemRemove_Click(object sender, RoutedEventArgs e)
{
if (medicineInventoryDataGrid.SelectedIndex != -1)
{
MedicineInventory M = (MedicineInventory)medicineInventoryDataGrid.SelectedItem;
MedicineInventory toRemove = (from a in ctx.MedicineInventories where (a.MedicineInventoryId == M.MedicineInventoryId) select a).Single();
ctx.MedicineInventories.Remove(toRemove);
}
}
However on clicking, I get the following error:
"The Specific Entity is not contained in the EntitySet."
Where am I going wrong?
try
MedicineInventory M = (MedicineInventory)medicineInventoryDataGrid.SelectedItem.DataItem;

Silverlight DataGrid scrollbar synchronization

I have 2 Silverlight DataGrids one on top of another. I want to synchronize their horizontal scrollbars.
I have tried to put them both in separate scrollviewers and set the horizontal offset of source scrollviewer to horizontal offset of target scrollviewer but that does not work, the below DataGrid scrollviewer disappers.I think that might be because these Datagrid are inside a StackPanel?
I also tried to put these 2 grids in a third grid and apply scrollviewer on that but that does not work either
Does anyone have an idea how to go about this?
Thanks a lot in advance
I did this in SL4, have no idea if it works in SL3, sorry. The docs state that the API is there but I have not tried it.
The trick is to use automation peers. Get the scroll pattern automation peers for both grids. When scrolling happens on one grid, scroll the other one through the automation peer.
To make this more concrete, assuming we have 2 grids, named _dgGrowth and _dgTotals:
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
public partial class MyPageWithGrids : Page {
...
private ScrollBar _sbGrowth, _sbTotals;
private AutomationPeer _peerGrowth, _peerTotals;
private bool _ignoreScrollEvents;
private void OnPageLoaded(object sender, RoutedEventArgs e) {
_sbGrowth = GetHorizontalScrollBar(_dgGrowth);
if (_sbGrowth != null) {
_sbGrowth.Scroll += OnScrollGrowthGrid;
}
_sbTotals = GetHorizontalScrollBar(_dgTotals);
if (_sbTotals != null) {
_sbTotals.Scroll += OnScrollTotalsGrid;
}
_peerGrowth = FrameworkElementAutomationPeer.CreatePeerForElement(_dgGrowth);
_peerTotals = FrameworkElementAutomationPeer.CreatePeerForElement(_dgTotals);
}
private ScrollBar GetHorizontalScrollBar(DataGrid parentGrid) {
return parentGrid.Descendents().OfType<ScrollBar>().FirstOrDefault(sb => sb.Name == "HorizontalScrollbar");
}
private void OnScrollTotalsGrid(object sender, ScrollEventArgs e) {
if (! _ignoreScrollEvents) {
SyncHorizontalScroll(_peerTotals, _peerGrowth);
}
}
private void OnScrollGrowthGrid(object sender, ScrollEventArgs e) {
if (! _ignoreScrollEvents) {
SyncHorizontalScroll(_peerGrowth, _peerTotals);
}
}
private void SyncHorizontalScroll(AutomationPeer source, AutomationPeer copy) {
IScrollProvider sourceProvider = null;
if (source != null) {
sourceProvider = (IScrollProvider) source.GetPattern(PatternInterface.Scroll);
}
IScrollProvider copyProvider = null;
if (copy != null) {
copyProvider = (IScrollProvider) copy.GetPattern(PatternInterface.Scroll);
}
if (sourceProvider != null && copyProvider != null) {
_ignoreScrollEvents = true;
// scroll copy at horizontal position of source, and keep vertical position
copyProvider.SetScrollPercent(sourceProvider.HorizontalScrollPercent, copyProvider.VerticalScrollPercent);
_ignoreScrollEvents = false;
}
}
}
What is not shown is setting up the Loaded event to OnPageLoaded and the Descendants() method found in this question.

Highligh a cell in a gridview

i have a dropdownlist with two values status- 'pending' and 'completed'. while im entering a new task my status is 'pending' once i finish it off i ll change my status as 'completed'. i have displayed it in gridview. the cell which i update as 'completed' have to be highlighted and the remaining cells in the status column i.e 'pending' has to be in another color
If a serverside callback is possible in this scenario, then subscribe the OnRowDataBound-Event and look for the specific row and set the css class of a label to something different. You could use a TemplateColumn with a Label in it.
E.g.
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == RowType.DataRow)
{
YourObject _item = (YourObject)e.Row.DataItem;
Literal _litFromTemplate = (Literal)e.Row.FindControl("litFromTemplate");
if(_item.Equals(anotherItem)) // or check for any other condition, like _item.Foo == 123
{
_litFromTemplate.CssClass = 'highlightingMe';
}
else
{
_litFromTemplate.CssClass = 'normalcssclass';
}
}
}

Resources