TableAdapter selected GridView Row and insert? - c#

I am trying to get something of a selected GridView Row which is controlled with TableAdapter. (Visual Studio 2010, ASP.NET)
I have otobas.xsd. Inside that, I have Otobus and Sefer tables and also SeferTableAdapter and OtobusTableAdapter. I fill the gridview like this;
otobasTableAdapters.SeferTableAdapter sef = new otobasTableAdapters.SeferTableAdapter();
otobas.SeferDataTable sefs;
sefs = sef.GetData();
SeferTableAdapter sefers = new SeferTableAdapter();
GridView1.DataSource = sefers.GetData();
GridView1.DataBind();
On the GridView Table, I have Select buttons on each row, therefore, when the user clicks one of the select buttons, I want to know which row it was. And then, I want to get the row like this (however, I can't)
otobas.SeferRow rw = ...?
I don't know how to do that. I tried many things but I couldn't. After I get this rw, I want to do something like;
string id = rw['id'];
string arrival = rw['arrival'];
However, I just couldn't do it. Can you show me a way to do this? I tried to add a row but I also couldn't do it. I don't know how to create a row I think. I tried to do the same as
http://msdn.microsoft.com/en-US/library/5ycd1034%28v=VS.80%29.aspx
but, when I wrote
otobas.SeferRow newRow = otobas... I have a data table named Sefer, but I can't reference it at all. Is it wrong to make an instance of the otobas dataset? Like
otobas oto = new otobas();
otobas.SeferRow sr = oto.Sefer.... ?
Why do I have to create an instance of otobas? I mean I already have it as otobas.xsd I just don't get it.

Haven't used this before but is it possible to implement an event handler for the onlclick event of this grid (in either client side or server side code) where you can make use of the event arguments to find this an other information you are searching for>?

Related

Updating the DataSource of a DropDownList in ASP.NET's C#

I have an edit form that uses a DropDownList, connected to a SQL server table using a DataSource. This edit form allows users to set the table's "IsDeleted" column value to 1, which would then hide it from all of the application's queries. (So the transaction still exists in the database, but not in the application)
The problem I've encountered with this is that if the page on which the edit form is is not left entirely, and then entered again, the entry still remains in the DataSource.
So essentially, the DataSource is not updating. (It's not running its select statement and repopulating until the whole page is reloaded). I've tried to use a page refresh and it doesn't seem to work, only going to a different page entirely and then coming back seems to update the DataSource.
How would I refresh the contents of the DataSource programmatically without having to recreate the entire DataSource itself?
You might want to add some code next time.
My best guess for now is that you're not doing anything OnPostback.
So I eventually resorted to doing what I did not want to do in the hopes that there would be a "Best-practice" way to do it (and one hopefully already built-in into DataSources), but it seems that was not to be.
All it took was to re-assign the DataSource's parameters to it again and then assign the DataSource back to the DropDownList, as if I were creating a new one.
I created the following method, which I then called at the end of my Delete button's event.
protected void DropDownList_Reload()
{
MyDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
MyDataSource.SelectCommand = "SELECT * FROM [tblMyTable] WHERE [IsDeleted] <> 1";
cbxDropDownList.DataSourceID = "MyDataSource";
cbxDropDownList.DataTextField = "MyHeader";
cbxDropDownList.DataValueField = "MyHeader";
}

Coded UI Test - Click Control Based On Other Things On Table Row

im making a Coded UI test in Visual Studio 2010 for a web application in C# and i wish to click a button in the 6th column of a table based on the inner text of column 1? is this possible?
So for instance i have a table with Names in column one and other info and then a button in column 6. All auto generated.
Im assuming if i can get the Row number from the cell with say "John Smith" in it i can then press the button for this row in column 6.
Any ideas? i have tried googling and looking at what parameters i can pass in but im coming up stumped.
Something based on the following may help.
Access the HTML table by copying code from that generated by the Coded UI recorder for an assertion or a click on the cells. You should have something like:
HtmlCell theTable = new HtmlCell(this.UItheWindow.UItheTable.UIItemTable);
Cells of the table can be accessed by adding properties such as:
theTable.FilterProperties[HtmlCell.PropertyNames.RowIndex] = "0";
theTable.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "6";
UITestControl cell = theTable.Find();
HtmlCell wantedCell = (HtmlCell)cell;
The above might be used as the body of a method returning the value of wantedCell. The name should now be available as:
wantedCell.InnerText;
Accessing the button to be clicked should follow a similar approach.
Another approach uses the GetChildren method to traverse the table. Start by getting theTable as above. Then have something like
UITestControlCollection children = theTable.GetChildren();
Loop through the children checking row properties. When the required row is found, call the GetChildren of that row and get the loop through them to find the required column. Some points: You may need to loop on columns before looping over rows. You may be able to directly index into the UITestControlCollection for the rows and the columns rather than needing to loop and check values. Depending on exactly how the table was written there may be additional levels between the table and the wanted cells, so you may need to examine children, grand children, great grand children, great great ..., and so on.
I have a couple of extension methods that I use to tackle content in tables(Hand coding, rather than using the recorder) -
This extension method for a table gets the first row in the table that contains the requested text in one of its cells or controls
public static HtmlRow GetRow(this HtmlTable table, string cellContent)
{
if((UITestControl)table == (UITestControl)null)
throw new ArgumentNullException("table");
if(cellContent.Length > 80)
cellContent = cellContent.Substring(0, 80); //Our table cells only display the first 80 chars
//Locate the first control in the table that has the inner text that I'm looking for
HtmlControl searchControl = new HtmlControl(table);
searchControl.SearchProperties.Add(PropertyNames.InnerText, cellContent);
//Did we find a control with that inner text?
if(!searchControl.TryFind())
{
//If not, the control might be an HtmlEdit with the text
HtmlEdit firstTableEdit = new HtmlEdit(table);
//Get all of the HtmlEdits in the table
UITestControlCollection matchingEdits = firstTableEdit.FindMatchingControls();
//Iterate through them, see if any have the correct text
foreach (UITestControl edit in matchingEdits)
{
if(cellContent == ((HtmlEdit)edit).Text)
searchControl = (HtmlControl)edit;
}
}
//We have(hopefully) found the control in the table with the text we're searching for
//Now, we spiral up through its parents until we get to an HtmlRow
while (!(searchControl is HtmlRow))
{
searchControl = (HtmlControl)searchControl.GetParent();
}
//The control we're left with should be an HtmlRow, and should be an Ancestor of a control that has the correct text
return (HtmlRow)searchControl;
}
Once you're able to get the correct row, it becomes relatively easy to get the correct control in that row(Or the correct control in a given cell in that row)
In your example, you've got a button in the 6th column of the row. The button probably has some properties associated with it, but even without them if we can correctly limit our search it will still work. Assume our table is named UITableCustomers - Declare a new button and limit it to only the 6th(Index 5) cell in the row containing "John Smith"
Mouse.Click(new HtmlInputButton(UITableCustomers.GetRow("John Smith").Cells[5]));
Obviously, this call is going to fail if the given text doesn't exist in a control in the table.
Your question is not very clear to me but check out the documentation on jQuery.trigger
This method will let you emulate a clickevent. I hope this is what you need.

How do I get bound winforms controls to refresh when their datasource is updated?

I'm very new to working with databases in C# (but not C# itself) and the whole concept of data binding, so please bear that in mind. I'm also using SQL Server CE if that affects this.
I'm trying to use a strongly-typed dataset built with VS2010 to access an SQL Server CE database, and bind WinForms controls to it to display data. I can get the controls bound fine, and they show the data that's currently in the database no problem. I can also insert new data into the database, and if I restart the application the inserted data shows up in the controls (specifically a ComboBox, but I'll be using more in the future).
The problem is that the new rows don't show up at all until I restart the app, and my understanding of data binding is that the controls should automagically update themselves. I've read about INotifyPropertyChanged but I'm not sure if it's the right thing to use here, and if it is, how do I use it for row insertions?
I'm binding the combobox like this:
DataSet.tagDataTable tagdata = new tagTableAdapter().GetData();
comboBox1.DataSource = tagdata;
comboBox1.DisplayMember = tagdata.tagnameColumn.ColumnName;
comboBox1.ValueMember = tagdata.tagIDColumn.ColumnName;
and inserting like this:
Guid g = Guid.NewGuid();
new tagTableAdapter().Insert(g, Name)
where Name is just a string pulled from a textbox.
Thanks.
EDIT: I should mention that the DB table I'm using is called tag, with two columns, tagID and tagname. tagID is a GUID, tagname is a varchar.
What you are doing with your TableAdpater.Insert is inserting your data directly into your database and you're circumventing any notifications in your application. If you do that then you have to do what iefpw said and that was to reload your datatable and rebind your controls.
An alternate method would be to add a row into your datatable. You can try something like this:
// retrieve the datatable from the control
DataSet.tagDataTable tagdata = comboBox1.DataSource as DataSet.tagDataTable;
// create a new row and fill in the data
var dr = tagdata.NewRow();
dr["tagid"] = Guid.NewGuid();
dr["tagname"] = Name;
// actually add it to the table
tagdata.Rows.Add(dr);
At this point you've added it to your DataTable and your combobox will automatically update, but the caveat is that it has not been written to your database so you'll need to make sure at some point you save to your database.
You should bind the data again and refresh the controls again like you did the first time. It doesn't just display the data. It is mechanic. It is not realtime.

ASP.NET GridView Can't hide Columns

So I have an <asp:Gridview> and in my C# file, I am setting the datasource to some database table, and doing .DataBind().
However, I want to hide a column in the table based on a Boolean variable.
Something like this:
gridview.Columns['Field5'].Visible = false;
Or perhaps:
int c = gridview.Rows.Count();
for(int i = 0; i < c; i++){
gridview.Rows['Field5'].Remove();
}
Perhaps I cannot make it invisible, but I'm sure I can at least loop through and remove all rows related to the column "field5". I don't know how to go about doing this.
Does anyone perhaps have a proper link to using the GridView Class and how all the methods are suppose to be used because it's not clear, perhaps not written by microsoft?
Sorry if this is simplistic, the internet seems to lack a lot of C# documentation (or maybe it's just cluttered with too much useless ASP.net information).
No problem, the trick is that you have to reference the column by its index, not its name.
grid.Columns[1].Visible = false;
In your case you should loop through gridview and set cells you want to hide Visible=false
foreach (GridViewRow gvr in gv.Rows)
{
//here specify cell you want to hide
//also you may put any conditions
gvr.Cells[0].Visible = false; // Hide cell
}
To hide column you should go like that:
//here specify column you want to hide
grv.Columns[0].Visible = false; // Hide column
Also check this article http://www.codeproject.com/KB/webforms/Datagrid_Col_Example.aspx
I found a good method for doing something like that.
((DataControlField)gridView.Columns
.Cast<DataControlField>()
.Where(fld => (fld.HeaderText == "Title"))
.SingleOrDefault()).Visible = false;
I found it in below link
GridView Hide Column by code
The gridview is fine if you just want to quickly display some data, but don't need to modify it in any way. However if you do want to customize it, it is unpractical. It is easier and faster to build the table dynamically in your codebehind and you get all of the control you need.
Check out the first answer to this post: How to show pop up menu from database in gridview on each gridview row items?

combobox default value winforms c#

Hi here i want to bind some values to check box dynamically.
dataset ds = //getting emp values form database;
cbemp.ValueMember = "empid";
cbemp.DisplayMember = "empname";
cbemp.DataSource = ds.Tables["emp"];
it is working fine. But i want to add
"Select emplyoee" as a default value of check box .
But my check box directly adding values like
a1
a2
a3
like this.
I tried like this
cbemp.Items.Insert(0, "Select emplyoee");
but it is not working
how can i add it?
When you use databinding, you cannot "manually" add or remove items. The only way to achieve what you want using databinding is to insert a row first in the DataTable with the desired value, or to populate the combobox by code (add the "Select employee" item and then iterate of the the DataTable rows to add the records).
Perhaps something like this could work:
// create new row for "Select employee"
DataRow row = ds.Tables["emp"].NewRow();
row["empid"] = -1;
row["empname"] = "Select employee";
// insert the row at the top of the table
ds.Tables["emp"].Rows.InsertAt(row, 0);
// do the databinding
cbemp.ValueMember = "empid";
cbemp.DisplayMember = "empname";
cbemp.DataSource = ds.Tables["emp"];
I don't use databinding much, so there may be drawbacks with this that I am unaware of (but I am confident that the community will point that out in that case).
Inserting data in your data source is a bad idea. It promotes breaking your layers' abstractions and may lead to some issues if you are using the same data source elsewhere.
Instead, you can easily extend the ComboBox to show a "Please select" message when it has no item selected.
I blogged about this issue and provided the code here : http://www.byteauthor.com/2010/08/inner-label-in-combobox/
I think you'd have to add it to the underlying datatable (ds.Tables["emp"]) for it to appear as an entry in the list when you're using databound controls.
When your control is data bound you cannot add items manually I think.
To work around this problem you could either add a new item to your data source, or you could add the items manually.
The above accepted solution is better, but one trick that might come in handy sometimes is to Union a "fake" record to the SQL that is returning the recordset for the data-binding. In this case, something like:
select 0 as empid, 'Please select' as empname
union
select empid, empname from emp
order by empid
Of course you will have to protect the database from accidentally writing the "0" record back (e.g. if the user doesn't make a selection), but that isn't too hard.
if ComboBoxStyle is set to DropDownList (so user cannot edit combobox) then the easiest way to make sure the user selects an item is to set selectedIndex=-1, you can always add "Please Select" etc ABOVE the combobox.
In my case ( databinded combobox ) i solved the problem like this. Of course the best way is the Kevin Coulombe one.
ComboBox.SelectedIndex = -1;
ComboBox.Text = "Please, select something";
With a bit of code you can manage this scenario pretty easy.

Categories

Resources