burhanahmed92 0 Light Poster

I have a multicolumn listview. There are three button Add, delete and Updata. List is working fine, it add, delete and update list data. But i want to save every row inside listview when i click save button. Bellow is my XAML.

<TextBox Canvas.Left="12" Canvas.Top="12" Height="23" Name="textBox1" 
        Width="120" TextChanged="textBox1_TextChanged" KeyDown="textBox1_KeyDown" 
        GotFocus="textBox1_GotFocus" />
    <TextBox Canvas.Left="146" Canvas.Top="12" Height="23" Name="textBox2" 
        Width="120" TextChanged="textBox2_TextChanged" GotFocus="textBox2_GotFocus" 
        KeyDown="textBox2_KeyDown" />
    <ListView Canvas.Left="12" Canvas.Top="68" Height="183" Name="listView1" 
        Width="253" SelectionChanged="listView1_SelectionChanged" 
        SelectionMode="Single">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Col1" Width="150" 
                    DisplayMemberBinding="{Binding Col1}"></GridViewColumn>
                <GridViewColumn Header="Col1" Width="50" 
                    DisplayMemberBinding="{Binding Col2}"></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <Button Name="addButton" Canvas.Left="122" Canvas.Top="41" Height="21" 
        Width="69" Click="addButton_Click" IsDefault="True">Add</Button>
    <Button Name="removeButton" Canvas.Left="197" Canvas.Top="41" Height="21" 
        Width="69" Click="removeButton_Click">Remove</Button>
    <Button Name="okButton" Canvas.Left="274" Canvas.Top="13" Content="OK" 
        Height="22" Width="74" Click="okButton_Click" />
    <Button Name="closeButton" Canvas.Left="274" Canvas.Top="44" Content="Close" 
        Height="22" Width="74" Click="closeButton_Click" />
    <Button Name="saveButton" Canvas.Left="274" Canvas.Top="75" Content="Save" 
        Height="22" Width="74" Click="saveButton_Click" />

My add method is like

private void addButton_Click(object sender, RoutedEventArgs e)
{
    setDataChanged(true);
    AddRow();
}

and other methods are also like these. I want a save method which get every row from listview and add each row to database. I am using SQL Server with WPF C#

Dani AI

Generated

— save the underlying data model, not the ListView visuals. Keep a simple POCO (e.g., RowModel) and an ObservableCollection<RowModel> bound to ListView.ItemsSource. Let your Add/Delete/Update change that collection; when the Save button is clicked, iterate the collection and persist rows. That approach avoids brittle UI-casting, is easy to test, and lets you use transactions and bulk operations safely.

Example: prepare a parameterized insert and run it on a background thread / with async so the UI does not block.

public class RowModel
{
    public string Col1 { get; set; }
    public int Col2 { get; set; }
}

private async Task SaveRowsAsync(IEnumerable<RowModel> rows, string connectionString)
{
    using (var conn = new SqlConnection(connectionString))
    {
        await conn.OpenAsync();
        using (var tx = conn.BeginTransaction())
        using (var cmd = conn.CreateCommand())
        {
            cmd.Transaction = tx;
            cmd.CommandText = "INSERT INTO YourTable (Col1, Col2) VALUES (@c1, @c2)";
            cmd.Parameters.Add(new SqlParameter("@c1", SqlDbType.NVarChar, 200));
            cmd.Parameters.Add(new SqlParameter("@c2", SqlDbType.Int));
            foreach (var r in rows)
            {
                cmd.Parameters["@c1"].Value = (object)r.Col1 ?? DBNull.Value;
                cmd.Parameters["@c2"].Value = r.Col2;
                await cmd.ExecuteNonQueryAsync();
            }
            tx.Commit();
        }
    }
}

For larger sets prefer SqlBulkCopy (build a DataTable from your collection and call WriteToServerAsync) — much faster and simpler than looping lots of individual inserts. Always use parameterized commands or bulk APIs, wrap operations in a transaction, store connection strings in config (not hard-coded), and handle exceptions to rollback and re-enable the UI. If you are not using binding and added items directly to ListView.Items, cast each ListView.Items entry back to your model type before saving.

Typical pitfalls: blocking the UI thread, concatenated SQL (SQL injection), failing to commit/rollback, and inserting duplicates (use a primary key or MERGE/upsert logic). These practices will make your Save button reliable and maintainable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.