I have filled listBox with data and assigning it to gridview but it doesn't. I verified by setting count of list to variable and it shows 3, perfect but after assigning it to gridview, the count of gridview shows 0. Why ?
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count; //returns 3
grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count; //return 0
}
}
Solved
protected void btnShowTempFeatures_Click(object sender, EventArgs e)
{
try
{
int count = ListBoxFeatures.Items.Count;
//grdViewTemporaryFeatures.DataSource = ListBoxFeatures.DataSource;
//grdViewTemporaryFeatures.DataBind();
int CountGrid= grdViewTemporaryFeatures.Rows.Count;
ListItemCollection lstTempFeatures = ListBoxFeatures.Items;
DataTable dTempFeatures = new DataTable();
dTempFeatures.Columns.Add("ID");
dTempFeatures.Columns.Add("FeatureName");
foreach (ListItem lstItem in lstTempFeatures)
{
DataRow dr = dTempFeatures.NewRow();
dr["ID"]= lstItem.Value;
dr["FeatureName"] = lstItem.Text;
dTempFeatures.Rows.Add(dr);
}
grdViewTemporaryFeatures.DataSource = dTempFeatures;
grdViewTemporaryFeatures.DataBind();
mdlTemporaryFeatures.Show();
}
Related
I have Gridview and I want to set click Event on the all cells in a Row ? How to Complete my Task ??
I tried this code but when i double click on the cell the new Form will come in Back / Behind of current Form. How to Show it on the Front ?
private void gridView2_DoubleClick(object sender, EventArgs e)
{
GridHitInfo celclick = gridView2.CalcHitInfo(gridControl2.PointToClient(Control.MousePosition));
if (celclick.InRow)
{
}
}
Please help me.
Use the GridView.RowCellClick event as follows:
gridControl1.DataSource = new List<Person> {
new Person(){ Name="John Smith"},
new Person(){ Name="Mary Smith"}
};
gridView1.OptionsBehavior.Editable = false; // disable editing
gridView1.RowCellClick += gridView1_RowCellClick;
//...
void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) {
if(e.Clicks == 2) { // Double Click
object cellValue = e.CellValue;
//do some stuff
}
}
//...
class Person {
public string Name { get; set; }
}
I have no Visual Studio here to test it but i think it should be something like this:
foreach(GridViewRow row in gridView2.Rows)
{
//Here you need something to get the cells out of row
cell.Click += (s, e) => { myClickEvent(c); };
}
Good luck
EDIT: Try this:
foreach(GridViewRow row in gridView2.Rows)
{
foreach (DataControlFieldCell cell in row.Cells)
{
cell.Click += (s, e) => { myClickEvent(c); };
}
}
Here is the solution:
DataTable dt = new DataTable();
private void B_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
int productId = (int)bt.Tag;
AddProductDataContext db = new AddProductDataContext();
decimal Quantity;
decimal.TryParse(txtCalculator.Text, out Quantity);
var results = from inv in db.Inventories
where inv.RecId == productId
select new
{
inventoryName = inv.InventoryName,
Quantity,
Total = Quantity * inv.InventoryPrice
};
foreach (var x in results)
{
DataRow newRow = dt.NewRow();
newRow.SetField("inventoryName", x.inventoryName);
newRow.SetField("Quantity", x.Quantity);
newRow.SetField("Total", x.Total);
dt.Rows.Add(newRow);
}
gridControl1.DataSource = dt;
}
private void SaleScreen_Load(object sender, EventArgs e)
{
dt.Columns.Add("inventoryName");
dt.Columns.Add("Quantity");
dt.Columns.Add("Total");
}
I solved the problem in this way. Thanks to everybody for their precious helps.
here is the code for my viewstate. but it's only store one value.what i need is it will keep the selected multiple values in the checkbox. this method is to keep/hold the value of check box in gridview of paging situation.
public void chkAssignee_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox selectBox = (CheckBox)sender;
GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent; // the row
GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview
string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString();
GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent;
int a = rowSelect.RowIndex;
ViewState["id"] = ID;
}
Try Following
Following Code may help u.
public void chkAssignee_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox selectBox = (CheckBox)sender;
GridViewRow myRow = (GridViewRow)selectBox.Parent.Parent; // the row
GridView myGrid = (GridView)myRow.Parent.Parent; // the gridview
string ID = myGrid.DataKeys[myRow.RowIndex].Value.ToString();
GridViewRow rowSelect = (GridViewRow)selectBox.Parent.Parent;
int a = rowSelect.RowIndex;
ArrayList SelecterdRowIndices=new ArrayList();
if(ViewState["SelectedRowIndices"]!=null)
{
SelecterdRowIndices=(ArrayList)ViewState["SelectedRowIndices"];
bool flag=false;
foreach (int i in SelecterdRowIndices)
{
if(i==Convert.ToInt32(ID))
{
flag=true;
break;
}
}
if(!flag)
{
SelecterdRowIndices.Add(ID);
}
}
else
{
SelecterdRowIndices.Add(ID);
}
ViewState["SelectedRowIndices"] = SelecterdRowIndices;
}
i've a grid on my page i need to refresh gridview add and delete new record but its not?
here is the code:
Add Row To GridView:
private void AddClientToGrid()
{
int clientID = int.Parse(ddlClient.SelectedValue);
int clientTypeID = int.Parse(ddlClientType.SelectedValue);
ClientsAllCDO client = new ClientsBL().ClientsAllSelectByIDAndClientTypeID(clientID, clientTypeID);
List<ClientsAllCDO> clientList = new List<ClientsAllCDO>();
clientList = GetClientsFromGrid();
clientList.Add(client);
gvClient.DataSource = clientList;
gvClient.DataBind();
}
Delete Code:
protected void btnDeleteClient_Click(object sender, EventArgs e)
{
LinkButton btnDeleteClient = sender as LinkButton;
int rowIndex = int.Parse(btnDeleteClient.Attributes["RowIndex"]);
if (Request.QueryString["BailiffID"] == null)
{
gvClient.DeleteRow(rowIndex);
}
else
{
int bailiffID = int.Parse(FormCrypto.Decrypt(Request.QueryString["BailiffID"]));
GridViewRow gvRow = gvClient.Rows[rowIndex];
int clientTypeID = int.Parse(((Label)gvRow.FindControl("lblClientTypeID")).Text);
int clientID = int.Parse(((Label)gvRow.FindControl("lblClientID")).Text);
gvClient.DeleteRow(rowIndex);
new BailiffClientsBL().BailiffClientDelete(clientID, bailiffID, clientTypeID);
}
}
Thanks alot...
You need to rebind the grid to the datasource:
//delete row from the database
GridView1.DataSource = SomeDataRetrievalMethod(); //retrieve the data from the database
GridView1.DataBind();
How can i get multiple selected rows in gridview using c# code & that selected rows i have to display in another form which also have gridview
public partial class WindowForm: Form
{
private DataTable dataTable = new DataTable();
//This will contain all the selected rows.
private List<DataGridViewRow> selectedRows = new List<DataGridViewRow>();
public WindowForm()
{
InitializeComponent();
dataTable .Columns.Add("Column1");
dataTable .Columns.Add("Column2");
dataTable .Columns.Add("Column3");
for (int i = 0; i < 30; i++)
{
dataTable .Rows.Add(i, "Row" + i.ToString(), "Item" + i.ToString());
}
dataGridView1.DataSource = dataTable ;
//This will select full row of a grid
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//This will allow multi selection
dataGridView1.MultiSelect = true;
dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);
dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
}
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
PerformSelection(dataGridView1, selectedRows);
}
void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (selectedRows.Contains(dataGridView1.CurrentRow))
{
selectedRows.Remove(dataGridView1.CurrentRow);
}
else
{
selectedRows.Add(dataGridView1.CurrentRow);
}
PerformSelection(this.dataGridView1, selectedRows);
}
private void PerformSelection(DataGridView dgv, List<DataGridViewRow> selectedRowsCollection)
{
foreach (DataGridViewRow dgvRow in dgv.Rows)
{
if (selectedRowsCollection.Contains(dgvRow))
{
dgvRow.Selected = true;
}
else
{
dgvRow.Selected = false;
}
}
}
}
I have a grideview and 2 buttons. I need to only show the buttons when the gridview has a selected item. My code looks like this:
protected void Page_Load(object sender, EventArgs e)
{
btactivate.Visible = false;
btdeactivate.Visible = false;
//Show Activate and Deactivate Buttons only if an item in the gridview is selected
if (GridView1.SelectedIndex != -1)
{
btactivate.Visible = true;
btdeactivate.Visible = true;
}
else
{
btactivate.Visible = false;
btdeactivate.Visible = false;
}
}
But the problem I have now is that only when I select the second time a item in the gridview the buttons show up. I need to have the the buttons show when I select the first time. I have tried changing the selected index to "-0" but that shows the buttons all the time (even when I dont have something selected). Can anyone please help?
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Columns.Add("Col3");
for (int i = 0; i < 20; i++)
{
DataRow dr = dt.NewRow();
dr["Col1"] = string.Format("Row{0}Col1", i + 1);
dr["Col2"] = string.Format("Row{0}Col2", i + 1);
dr["Col3"] = string.Format("Row{0}Col3", i + 1);
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
SetButtonState();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
SetButtonState();
}
private void SetButtonState()
{
btactivate.Visible = GridView1.SelectedIndex > -1;
btdeactivate.Visible = GridView1.SelectedIndex > -1;
}